48 lines
1007 B
Nix
48 lines
1007 B
Nix
{ pkgs, lib }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
input = fileContents ./input.txt;
|
|
|
|
answer1 = lib.pipe input [
|
|
(split "mul\\(([[:digit:]]{1,3}),([[:digit:]]{1,3})\\)")
|
|
(lib.filter isList)
|
|
(map (map toInt))
|
|
(map (xs: (head xs) * (last xs)))
|
|
(foldl add 0)
|
|
toString
|
|
];
|
|
|
|
answer2 = lib.pipe input [
|
|
(split "mul\\(([[:digit:]]{1,3}),([[:digit:]]{1,3})\\)|(do\\(\\))|(don't\\(\\))")
|
|
(lib.filter isList)
|
|
(map (xs:
|
|
if elemAt xs 0 != null
|
|
then (toInt (elemAt xs 0)) * (toInt (elemAt xs 1))
|
|
else if elemAt xs 2 != null
|
|
then "do"
|
|
else if elemAt xs 3 != null
|
|
then "dont"
|
|
else null
|
|
))
|
|
(foldl ({ toggle, sum }: x: {
|
|
toggle = if x == "do" then true
|
|
else if x == "dont" then false
|
|
else toggle;
|
|
sum = sum + (if toggle && isInt x then x else 0);
|
|
}) {
|
|
toggle = true;
|
|
sum = 0;
|
|
})
|
|
(x: toString x.sum)
|
|
];
|
|
in
|
|
pkgs.writeText "answers" ''
|
|
Task1:
|
|
${answer1}
|
|
|
|
Task2:
|
|
${answer2}
|
|
''
|