-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2b.js
57 lines (43 loc) · 1.38 KB
/
2b.js
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
import {readInput} from "./lib.js";
import * as assert from "node:assert";
const input = readInput(2)
assert.ok(!isSafe('1 2 1'.split(' '))); // up then down
assert.ok(!isSafe('1 2 2'.split(' '))); // no inc
assert.ok(!isSafe('1 2 10'.split(' '))); // big inc
assert.ok(!isSafe('1 2 1'.split(' '))); // down then up
assert.ok(!isSafe('2 1 1'.split(' '))); // no dec
assert.ok(!isSafe('10 9 1'.split(' '))); // big inc
assert.ok(!isSafe('14 13 14 12 15 18 15'.split(' '))); // down up down
const answer = input
.map((line) => {
return line.split(' ').map((x) => parseInt(x));
})
.filter((report) => {
if (isSafe(report)) {
return true;
}
for (let i = 0; i < report.length; i++) {
const left = report.slice(0, i);
const right = report.slice(i + 1);
if (isSafe([...left, ...right])) {
return true;
}
}
return false;
})
.length;
function isSafe(report) {
const [first, second] = report;
const up = first < second;
for (let i = 1; i < report.length; i++) {
const lvl = report[i];
const prevLvl = report[i - 1];
let difference = lvl - prevLvl;
difference = up ? difference : -difference;
if (difference > 3 || difference <= 0) {
return false;
}
}
return true;
}
console.log(answer);