advent-of-code/2024/day02/default.nix
2024-12-02 16:19:17 +01:00

41 lines
901 B
Nix

{ pkgs, lib }:
with lib;
let
input = pipe (fileContents ./input.txt) [
(splitString "\n")
(map (split "[[:space:]]+"))
(map (filter (x: x != [])))
(map (map toInt))
];
# - The levels are either all increasing or all decreasing.
# - Any two adjacent levels differ by at least one and at most three.
safeCondition = xs: let
zipped = zipLists xs (tail xs);
in (all ({ fst, snd }: let x = fst - snd; in 1 <= x && x <= 3) zipped)
|| (all ({ fst, snd }: let x = fst - snd; in -1 >= x && x >= -3) zipped);
answer1 = lib.pipe input [
(filter safeCondition)
length
toString
];
answer2 = lib.pipe input [
(filter (xs: let
variants = genList (i: (take i xs) ++ (drop (i + 1) xs)) (length xs);
in any safeCondition variants))
length
toString
];
in
pkgs.writeText "answers" ''
Task1:
${answer1}
Task2:
${answer2}
''