From 763be9b5899eda3997c5c45c6cbee64960e64334 Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Mon, 2 Dec 2024 22:49:37 +1100 Subject: [PATCH] Day 2 Puzzle 1 --- day2.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 day2.ts diff --git a/day2.ts b/day2.ts new file mode 100644 index 0000000..5750c3e --- /dev/null +++ b/day2.ts @@ -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());