From bef73c0cb5f57e6bc74c99e93995481312d5100b Mon Sep 17 00:00:00 2001 From: Tyler Beckman Date: Mon, 2 Dec 2024 03:22:49 -0700 Subject: [PATCH] day 2 --- day2/part1.ts | 17 +++++++++++++++++ day2/part2.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 day2/part1.ts create mode 100644 day2/part2.ts diff --git a/day2/part1.ts b/day2/part1.ts new file mode 100644 index 0000000..43251a7 --- /dev/null +++ b/day2/part1.ts @@ -0,0 +1,17 @@ +export async function solve() { + return await Deno.readTextFile("./input.txt").then((f) => + f.split("\n").filter((l) => { + const report = l.split(" ").map((s) => +s); + const increasing = report[0] < report[1]; + let last = report[0]; + for (const number of report.slice(1)) { + if (number > last && !increasing) return false; + if (number < last && increasing) return false; + if (Math.abs(number - last) < 1 || Math.abs(number - last) > 3) return false; + + last = number; + } + return true; + }).reduce(acc => acc + 1, 0) + ); +} \ No newline at end of file diff --git a/day2/part2.ts b/day2/part2.ts new file mode 100644 index 0000000..c8d8563 --- /dev/null +++ b/day2/part2.ts @@ -0,0 +1,29 @@ +export async function solve() { + return await Deno.readTextFile("./input.txt").then((f) => + f.split("\n").filter((l) => { + const report = l.split(" ").map((s) => +s); + for (let i = 0; i < report.length; i++) { + const sliced = [ + ...report.slice(0, i), + ...report.slice(i + 1, report.length), + ]; + const increasing = sliced[0] < sliced[1]; + let last = sliced[0]; + let failed = false; + for (const number of sliced.slice(1)) { + if (number > last && !increasing) failed = true; + if (number < last && increasing) failed = true; + if ( + Math.abs(number - last) < 1 || + Math.abs(number - last) > 3 + ) failed = true; + + last = number; + } + if (failed) continue; + else return true; + } + return false; + }).reduce((acc) => acc + 1, 0) + ); +}