This commit is contained in:
Grimmauld 2024-12-02 10:05:27 +01:00
parent bb768f9ae5
commit 315d8a4aa3
Signed by: Grimmauld
SSH key fingerprint: SHA256:Q8IL6Y7sSKqzkyFdV1L0O/EflEh1fFV3tBtwxpapRH4
2 changed files with 15 additions and 4 deletions

View file

@ -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;

View file

@ -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;
}