Advent-of-Nix-2024/day3/default.nix

17 lines
696 B
Nix

{ lib, utils, ... }:
with lib;
with builtins;
with utils;
let
input = readFile ./input;
getFactorsInBlock = s:
map (map toInt)
(matchAll "mul\\(([[:digit:]]{1,3}),([[:digit:]]{1,3})\\)" s);
sumInBlock = block:
listSum (map (l: (head l) * (last l)) (getFactorsInBlock block));
in {
part1 = sumInBlock input;
part2 = listSum (map sumInBlock
(map (do: head (reSplit "don't\\(\\)" do)) (reSplit "do\\(\\)" input)));
part2oneline = foldl' add 0 (map (pipe' [ (matchAll "mul\\(([[:digit:]]{1,3}),([[:digit:]]{1,3})\\)") (foldl' (p: l: p + (head l |> toInt) * (last l |> toInt)) 0) ]) (map (pipe' [(reSplit "don't\\(\\)") head ]) (reSplit "do\\(\\)" (readFile ./input))));
}