adventofcode/2024/02/part1.ts

35 lines
886 B
TypeScript
Raw Normal View History

2024-12-04 20:46:05 +00:00
import * as fs from "node:fs/promises";
export function readInput(filename: string): Promise<number[][]> {
return fs
.readFile(filename, { encoding: "utf8" })
.then((lines: string) => lines.split(/\n/))
.then((lines: string[]) =>
lines.filter((x) => x).map((x) =>
x.split(/ +/).map((x) => Number(x))
)
);
}
export function isSafe(report: readonly number[]): boolean {
const r = [ ...report ];
const asc = r[1] - r[0];
let level = r.shift();
while (r.length) {
const next = r.shift();
const delta = next! - level!;
if (delta * asc < 0) return false;
if (Math.abs(delta) < 1 || Math.abs(delta) > 3) return false;
level = next;
}
return true;
}
export function solve(reports: number[][]): number {
return reports.filter(isSafe).length;
}