-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.js
51 lines (45 loc) · 1.12 KB
/
part1.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
const { readFileSync } = require("fs");
// Parse Input
const inputFile = "input.txt";
let reports = [];
const input = readFileSync(inputFile).toString();
input.split("\n").forEach((line) => {
if (line.split(" ").length > 1) {
reports.push(line.split(" ").map((x) => parseInt(x)));
}
});
// Compute Safe reports
let result = 0;
reports.forEach((r) => {
let current = r[0];
let gap = 0;
let valid = true;
r.slice(1).forEach((x) => {
if (valid == false) return;
if (gap == 0) {
if (current + 1 == x || current + 2 == x || current + 3 == x) {
gap = 1;
current = x;
} else if (current - 1 == x || current - 2 == x || current - 3 == x) {
gap = -1;
current = x;
} else {
valid = false;
}
} else if (gap > 0) {
if (current + 1 == x || current + 2 == x || current + 3 == x) {
current = x;
} else {
valid = false;
}
} else {
if (current - 1 == x || current - 2 == x || current - 3 == x) {
current = x;
} else {
valid = false;
}
}
});
if (valid) result++;
});
console.log(result);