Day 2 Puzzle 1
This commit is contained in:
parent
ee66a68efb
commit
763be9b589
1 changed files with 22 additions and 0 deletions
22
day2.ts
Normal file
22
day2.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
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());
|
Loading…
Reference in a new issue