From 8bf91cfc9a1d15c10e3d2e77cfe2697de53549f2 Mon Sep 17 00:00:00 2001 From: Andres Date: Tue, 3 Dec 2024 12:37:07 +0100 Subject: [PATCH] day 2 (refactor) --- day-2/day2.ts | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/day-2/day2.ts b/day-2/day2.ts index b291ca3..c294773 100644 --- a/day-2/day2.ts +++ b/day-2/day2.ts @@ -1,3 +1,5 @@ +import * as R from "remeda"; + export async function day2(input: string) { const lines = input.split("\n"); let count = 0; @@ -44,33 +46,10 @@ export async function day2part2(input: string) { } function isSafe(levels: number[]) { - let increasing = null; - let safe = true; + const diffs = R.zip(levels, levels.slice(1)).map(([a, b]) => a - b); - for (let i = 0; i < levels.length - 1; i++) { - const current = levels[i]; - const next = levels[i + 1]; - const diff = Math.abs(next - current); - - if (diff > 3 || diff < 1) { - safe = false; - break; - } - - if (i === 0) { - increasing = next > current; - continue; - } - - if (increasing && next <= current) { - safe = false; - break; - } - - if (!increasing && next >= current) { - safe = false; - break; - } - } - return safe; + return ( + diffs.every((d) => 1 <= d && d <= 3) || + diffs.every((d) => -1 >= d && d >= -3) + ); }