-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
40 lines (35 loc) · 936 Bytes
/
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
import { zip } from 'lodash';
import {
INPUT_PATH,
runner,
SAMPLE_PATH,
splitLines,
sum,
} from '../../../lib/utils';
const path = `${__dirname}/${INPUT_PATH}`;
const getSafeLevel = (lines: number[][]) => {
return lines
.map((line) => zip(line, line.slice(1)).slice(0, -1) as number[][])
.filter((pairs) => {
const diffs = pairs.map(([x, y]) => x - y);
return (
diffs.every((diff) => diff <= 3 && diff > 0) ||
diffs.every((diff) => diff >= -3 && diff < 0)
);
}).length;
};
const tryGetSafeLevel = (lines: number[][]) => {
return lines.filter((line) =>
getSafeLevel(
line.map((_, idx) => line.slice(0, idx).concat(line.slice(idx + 1))),
),
).length;
};
// Part 1
runner({
path,
solution: (input) => {
const lines = splitLines(input).map((e) => e.split(' ').map(Number));
return { p1: getSafeLevel(lines), p2: tryGetSafeLevel(lines) };
},
});