-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
54 lines (44 loc) · 1.48 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
interface Section {
start: number;
end: number;
}
interface AssignmentPair {
firstSection: Section;
secondSection: Section;
}
function parseAssignmentPair(assignmentPair: string): AssignmentPair {
const { groups } =
assignmentPair.match(
/(?<firstSectionStart>\d+)-(?<firstSectionEnd>\d+),(?<secondSectionStart>\d+)-(?<secondSectionEnd>\d+)/,
) || [];
if (!groups) {
throw new Error('Invalid Input');
}
const { firstSectionStart, firstSectionEnd, secondSectionStart, secondSectionEnd } = groups;
return {
firstSection: {
start: parseInt(firstSectionStart, 10),
end: parseInt(firstSectionEnd, 10),
},
secondSection: {
start: parseInt(secondSectionStart, 10),
end: parseInt(secondSectionEnd, 10),
},
};
}
function part1(assignmentPairs: string[]): number {
return assignmentPairs.filter((assignmentPair) => {
const { firstSection, secondSection } = parseAssignmentPair(assignmentPair);
return (
(firstSection.start <= secondSection.start && firstSection.end >= secondSection.end) ||
(firstSection.start >= secondSection.start && firstSection.end <= secondSection.end)
);
}).length;
}
function part2(assignmentPairs: string[]): number {
return assignmentPairs.filter((assignmentPair) => {
const { firstSection, secondSection } = parseAssignmentPair(assignmentPair);
return firstSection.end >= secondSection.start && secondSection.end >= firstSection.start;
}).length;
}
export { part1, part2 };