-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
76 lines (68 loc) · 1.3 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import run from "aocrunner";
const parseInput = (rawInput: string) => rawInput.split("\n");
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
return input.reduce((total, row) => {
const [aStart, aStop, bStart, bStop] = row.split(/[,-]/).map(Number);
if (aStart <= bStart && aStop >= bStop) {
return total + 1;
}
if (bStart <= aStart && bStop >= aStop) {
return total + 1;
}
return total;
}, 0);
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
return input.reduce((total, row) => {
const [aStart, aStop, bStart, bStop] = row.split(/[,-]/).map(Number);
if (aStop >= bStart && aStop <= bStop) {
return total + 1;
}
if (bStop >= aStart && bStop <= aStop) {
return total + 1;
}
return total;
}, 0);
};
run({
part1: {
tests: [
{
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`,
expected: 2,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `17-92,7-17
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8`,
expected: 5,
},
{
input: `3-99,2-98
23-92,23-24
92-92,19-93
3-94,3-96`,
expected: 4,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});