2024: init
This commit is contained in:
40
2024/day02/default.nix
Normal file
40
2024/day02/default.nix
Normal file
@@ -0,0 +1,40 @@
|
||||
{ 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}
|
||||
''
|
||||
1000
2024/day02/input.txt
Normal file
1000
2024/day02/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user