-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.js
executable file
·68 lines (60 loc) · 1.86 KB
/
day04.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
// https://adventofcode.com/2022/day/4
const input = `2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8`;
// Part 1 - Find fully overlapping ranges
function fullOverlap(leftStart, leftEnd, rightStart, rightEnd) {
// Case 1 - right range inside of left
if (leftStart <= rightStart && rightEnd <= leftEnd) {
return true;
}
// Case 2 - left range inside of right
if (rightStart <= leftStart && leftEnd <= rightEnd) {
return true;
}
return false;
}
let sum = 0;
for (const line of input.split("\n")) {
const match = line.match(/(\d+)-(\d+),(\d+)-(\d+)/);
const [leftStart, leftEnd, rightStart, rightEnd] =
[Number(match[1]), Number(match[2]), Number(match[3]), Number(match[4])];
if (fullOverlap(leftStart, leftEnd, rightStart, rightEnd)) {
sum++;
}
}
console.log(sum); // 599
// Part 2 - Find all overlapping ranges
function partialOverlap(leftStart, leftEnd, rightStart, rightEnd) {
// Case 1 - right range starts inside left range
if (leftStart <= rightStart && rightStart <= leftEnd) {
return true;
}
// Case 2 - right range ends inside left range
if (leftStart <= rightEnd && rightEnd <= leftEnd) {
return true;
}
// Case 3 - left range starts inside right range
if (rightStart <= leftStart && leftStart <= rightEnd) {
return true;
}
// Case 4 - left range ends inside right range
if (rightStart <= leftEnd && leftEnd <= rightEnd) {
return true;
}
return false;
}
sum = 0;
for (const line of input.split("\n")) {
const match = line.match(/(\d+)-(\d+),(\d+)-(\d+)/);
const [leftStart, leftEnd, rightStart, rightEnd] =
[Number(match[1]), Number(match[2]), Number(match[3]), Number(match[4])];
if (partialOverlap(leftStart, leftEnd, rightStart, rightEnd)) {
sum++;
}
}
console.log(sum);