Advent-of-Nix-2024/day2/default.nix
2024-12-02 10:00:36 +01:00

13 lines
592 B
Nix

{ lib, utils, ... }:
with lib;
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))));
in {
part1 = count isSafe records;
part2 = count isSafeDampened records;
}