aoc/2024/03/solution.nix

58 lines
1.2 KiB
Nix
Raw Permalink Normal View History

2024-12-10 09:04:17 +00:00
{lib, ...}: content: rec {
2024-12-03 15:05:41 +00:00
muls = memory: memory
|> builtins.split ''mul\(([0-9]{0,3}),([0-9]{0,3})\)''
|> builtins.filter builtins.isList
;
mul = builtins.foldl'
(acc: n: let
nums = map lib.strings.toInt n;
in acc + ( builtins.elemAt nums 0 ) * ( builtins.elemAt nums 1 ))
0
;
part1result = content |> muls |> mul;
2024-12-03 15:28:09 +00:00
conditionsAndMuls = memory: let
mulMatcher = ''mul\(([0-9]{0,3}),([0-9]{0,3})\)'';
conditionMatcher = ''(do|don't)\(\)'';
in memory
|> builtins.split "${mulMatcher}|${conditionMatcher}"
|> builtins.filter builtins.isList
|> map toInstruction
;
toInstruction = matches: let
elem = builtins.elemAt matches;
numOrZero = v: if isNull v
then 0
else lib.strings.toInt v;
in {
cmd = let todo = elem 2; in
if isNull todo then "mul" else todo;
a = numOrZero (elem 0);
b = numOrZero (elem 1);
};
conditionalMul = builtins.foldl'
({mul, acc}: n: {
acc = acc + mul * n.a * n.b;
mul = if n.cmd == "do" then 1
else if n.cmd == "don't" then 0
else mul;
})
{
mul = 1;
acc = 0;
}
;
part2result = content
|> conditionsAndMuls
|> conditionalMul
|> (r: r.acc)
;
2024-12-03 15:05:41 +00:00
}