forked from remipou/aoc22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4dec.ts
28 lines (23 loc) · 992 Bytes
/
4dec.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
import getData from "./data";
const [_ts, _file, date, test] = process.argv;
const data: string[] = getData(date, test).split("\n");
const isFullyContained = (a: string, b: string, overlap?: boolean) => {
const aMin = Number(a.substring(0, a.indexOf("-")));
const bMin = Number(b.substring(0, b.indexOf("-")));
const aMax = Number(a.substring(a.indexOf("-") + 1, a.length));
const bMax = Number(b.substring(b.indexOf("-") + 1, b.length));
const min = Math.min(aMin, bMin);
const max = Math.max(aMax, bMax);
return overlap
? !Boolean(aMin > bMax || bMin > aMax)
: (min === aMin && max === aMax) || (min === bMin && max === bMax);
};
console.time("part1");
const array = data.map((d) => d.split(","));
const part1 = array.filter((d) => isFullyContained(d[0], d[1])).length;
console.log(part1);
console.timeEnd("part1");
console.time("part2");
const part2 = array.filter((d) => isFullyContained(d[0], d[1], true)).length;
console.log(part2);
console.timeEnd("part2");