2024-12-02 06:49:37 -05:00
|
|
|
import fs from "node:fs";
|
|
|
|
|
|
|
|
const input = fs.readFileSync("inputs/day2").toString();
|
|
|
|
|
|
|
|
const rows = input.split("\n").filter(Boolean);
|
|
|
|
|
|
|
|
function safe_levels(_row: number[]) {
|
|
|
|
const row = [..._row];
|
|
|
|
while (row.length > 1) {
|
|
|
|
const diff = row[1] - row[0];
|
|
|
|
if (diff > 3 || diff < 1) return false;
|
|
|
|
row.shift();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function puzzle1() {
|
|
|
|
return rows.map((row) => row.split(/\s+/g).map(i => +i))
|
|
|
|
.filter(row => safe_levels(row) || safe_levels(row.toReversed())).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Puzzle 1", puzzle1());
|
2024-12-03 21:49:33 -05:00
|
|
|
|
|
|
|
function dampen_lists(row: number[]) {
|
|
|
|
const rows: number[][] = new Array(row.length).fill(row).map((_row, i) => {
|
|
|
|
const row = [..._row];
|
|
|
|
row.splice(i, 1);
|
|
|
|
return row;
|
|
|
|
});
|
|
|
|
return [row, ...rows].filter(safe_levels).length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function puzzle2() {
|
|
|
|
return rows.map((row) => row.split(/\s+/g).map(i => +i))
|
|
|
|
.filter(row => dampen_lists(row) || dampen_lists(row.toReversed())).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Puzzle 2", puzzle2());
|