-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
59 lines (49 loc) · 1.29 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
import run from "aocrunner";
import { lines, tuple } from "../utils/index.js";
import { map, max, min, sum, toNumber } from "lodash-es";
const parseInput = lines(tuple(tuple(toNumber, toNumber, '-'), tuple(toNumber, toNumber, '-'), ','));
const isInside = (left: [number, number], right: [number, number]) => {
return left[0] <= right[0] && right[1] <= left[1];
}
const isInsideBi = (left: [number, number], right: [number, number]) => {
return isInside(left,right) || isInside(right, left);
}
const overlaps = (left: [number, number], right: [number, number]) => {
return Math.max(left[0], right[0])<= Math.min(left[1], right[1]);
}
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
return sum(map(input, ([x,y]) => isInsideBi(x,y) ? 1 : 0))
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
return sum(map(input, ([x,y]) => overlaps(x,y) ? 1 : 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: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});