-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.ts
47 lines (44 loc) · 1.14 KB
/
day2.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
const p = console.log;
const reports = (await Deno.readTextFile("2.in")).trim().split("\n")
.map((line) => line.split(" ").map((e) => parseInt(e)));
const isSafe = (r) => {
const up = r[1] - r[0] > 0 ? 1 : -1;
for (let i = 0; i < r.length - 1; i++) {
const diff = r[i + 1] - r[i];
if ((diff * up) < 0 || Math.abs(diff) < 1 || Math.abs(diff) > 3) {
return [i, i + 1];
}
}
return [];
};
const p1 = reports //.slice(999)
.map((r) => isSafe(r).length == 0 ? 1 : 0)
.reduce((a, b) => a + b);
p({ p1 });
const p2 = reports //.slice(999)
.map((r) => {
const ans1 = isSafe(r);
if (ans1.length == 0) {
return 1;
} else {
const r0 = r.slice();
if (ans1[0] !== 0) {
r0.splice(ans1[0] - 1, 1);
}
const r1 = r.slice();
r1.splice(ans1[0], 1);
const r2 = r.slice();
r2.splice(ans1[1], 1);
//p({ans1, r1, r2})
if (
(ans1[0] !== 0 && isSafe(r0).length == 0) || isSafe(r1).length == 0 ||
isSafe(r2).length == 0
) {
return 1;
} else {
//p('no match')
return 0;
}
}
}).reduce((a, b) => a + b);
p({ p2 });