From 315d8a4aa3764ba54a8e8fb840c3adcace9b4bae Mon Sep 17 00:00:00 2001 From: Grimmauld Date: Mon, 2 Dec 2024 10:05:27 +0100 Subject: [PATCH] fmt --- day2/default.nix | 17 ++++++++++++++--- utils.nix | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/day2/default.nix b/day2/default.nix index 68c2c11..e74c200 100644 --- a/day2/default.nix +++ b/day2/default.nix @@ -4,9 +4,20 @@ with builtins; with utils; let records = map (l: map toInt (splitWhitespace l)) (readLines ./input); - getMaxDelta = l: (foldl' (prev: value: { inherit value; maxDelta = max prev.maxDelta (delta prev.value value); } ) { value = head l; maxDelta = 0; } l).maxDelta; - isSafe = r: let sorted = sort lessThan r; in (r == sorted || r == (reverseList sorted)) && allUnique r && (3 >= getMaxDelta r); - isSafeDampened = r: any isSafe ([ r ] ++ (map (deleteAt r) (range 0 (length r)))); + getMaxDelta = l: + (foldl' (prev: value: { + inherit value; + maxDelta = max prev.maxDelta (delta prev.value value); + }) { + value = head l; + maxDelta = 0; + } l).maxDelta; + isSafe = r: + let sorted = sort lessThan r; + in (r == sorted || r == (reverseList sorted)) && allUnique r + && (3 >= getMaxDelta r); + isSafeDampened = r: + any isSafe ([ r ] ++ (map (deleteAt r) (range 0 (length r)))); in { part1 = count isSafe records; part2 = count isSafeDampened records; diff --git a/utils.nix b/utils.nix index c768c72..bcc0236 100644 --- a/utils.nix +++ b/utils.nix @@ -6,7 +6,7 @@ with builtins; rec { abs = i: if i >= 0 then i else i * -1; delta = x: y: abs (x - y); listSum = foldl' add 0; - deleteAt = l: i: (take i l) ++ (drop (i+1) l); + deleteAt = l: i: (take i l) ++ (drop (i + 1) l); eq = x: y: x == y; greaterThan = x: y: x > y; }