-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.ts
32 lines (28 loc) · 957 Bytes
/
02.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
export function getSafeReportCount(
reports: Array<Array<number>>,
): number {
let result = 0;
for (const report of reports) {
let i = 0;
const diffs: Array<number> = [];
while (i < report.length - 1) {
const current = report[i];
const next = report[++i];
diffs.push(current - next);
}
const isDiffInRange = diffs.map(Math.abs).every((n) => n >= 1 && n <= 3);
const isIncreasing = diffs.map(Math.sign).every((n) => n === -1);
const isDecreasing = diffs.map(Math.sign).every((n) => n === 1);
result += Number(isDiffInRange && (isIncreasing || isDecreasing));
}
return result;
}
if (import.meta.main) {
const input = await Deno.readTextFile("02_input.txt");
const reports: Array<Array<number>> = [];
for (const line of input.split("\n")) {
const report = line.split(/\s/).map(Number);
reports.push(report);
}
console.log("# of safe reports = ", getSafeReportCount(reports));
}