This commit is contained in:
Grimmauld 2024-12-01 15:59:47 +01:00
parent 4f3ad6d920
commit 9790098496
Signed by: Grimmauld
SSH key fingerprint: SHA256:Q8IL6Y7sSKqzkyFdV1L0O/EflEh1fFV3tBtwxpapRH4
2 changed files with 4 additions and 4 deletions

View file

@ -4,9 +4,9 @@ with builtins;
with utils;
let
numbers = map (l: map toInt (splitWhitespace l)) (readLines ./input);
left = sortOn noop (map (flip elemAt 0) numbers);
right = sortOn noop (map (flip elemAt 1) numbers);
left = sort lessThan (map (flip elemAt 0) numbers);
right = sort lessThan (map (flip elemAt 1) numbers);
in {
part1 = listSum (zipListsWith delta left right);
part2 = listSum (map (x: x * (count (y: x == y) right)) left);
part2 = listSum (map (x: x * (count (eq x) right)) left);
}

View file

@ -5,6 +5,6 @@ with builtins; rec {
splitWhitespace = s: filter isString (builtins.split "[[:space:]]+" s);
abs = i: if i >= 0 then i else i * -1;
delta = x: y: abs (x - y);
noop = x: x;
listSum = foldl' add 0;
eq = x: y: x == y;
}