2022-12-12 13:57:34 +01:00
|
|
|
{ pkgs, lib, AoCLib, ... }:
|
2022-12-03 13:12:25 +01:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
2022-12-12 13:57:34 +01:00
|
|
|
inherit (AoCLib) transformRange chunksOf;
|
|
|
|
|
2022-12-03 13:12:25 +01:00
|
|
|
compartments = pipe (fileContents ./input.txt) [
|
|
|
|
(splitString "\n")
|
|
|
|
];
|
|
|
|
|
|
|
|
splitAtMiddle = s: {
|
|
|
|
c1 = substring 0 ((stringLength s) / 2) s;
|
|
|
|
c2 = substring ((stringLength s) / 2) (stringLength s) s;
|
|
|
|
};
|
2022-12-12 13:57:34 +01:00
|
|
|
|
2022-12-03 13:12:25 +01:00
|
|
|
charsToSet = s: foldl (set: c: set // { ${c} = true; }) { } (stringToCharacters s);
|
2022-12-12 13:57:34 +01:00
|
|
|
|
2022-12-03 13:12:25 +01:00
|
|
|
getCommonChar = { c1, c2 }:
|
|
|
|
findSingle
|
|
|
|
(c: c2.${c} or false)
|
|
|
|
"Error: no common chars"
|
|
|
|
"Error: multiple chars"
|
|
|
|
(attrNames c1);
|
|
|
|
|
|
|
|
charValue = c: pipe c [
|
|
|
|
lib.strings.charToInt
|
|
|
|
(transformRange 65 91 (-(65 - 27)))
|
|
|
|
(transformRange 97 123 (-(97 - 1)))
|
|
|
|
];
|
|
|
|
|
|
|
|
answer1 = pipe compartments [
|
|
|
|
(map splitAtMiddle)
|
|
|
|
(map (mapAttrs (_: charsToSet)))
|
|
|
|
(map getCommonChar)
|
|
|
|
(map charValue)
|
|
|
|
(foldr add 0)
|
|
|
|
toString
|
|
|
|
];
|
|
|
|
|
|
|
|
toA123 = l: {
|
|
|
|
a1 = elemAt l 0;
|
|
|
|
a2 = elemAt l 1;
|
|
|
|
a3 = elemAt l 2;
|
|
|
|
};
|
2022-12-12 13:57:34 +01:00
|
|
|
|
2022-12-03 13:12:25 +01:00
|
|
|
getCommonChar2 = { a1, a2, a3 }:
|
|
|
|
findSingle
|
|
|
|
(c: a2.${c} or false && a3.${c} or false)
|
|
|
|
"Error: no common chars"
|
|
|
|
"Error: multiple chars"
|
|
|
|
(attrNames a1);
|
|
|
|
|
|
|
|
answer2 = pipe compartments [
|
|
|
|
(chunksOf 3)
|
|
|
|
(map toA123)
|
|
|
|
(map (mapAttrs (_: charsToSet)))
|
|
|
|
(map getCommonChar2)
|
|
|
|
(map charValue)
|
|
|
|
(foldr add 0)
|
|
|
|
toString
|
|
|
|
];
|
|
|
|
|
|
|
|
in pkgs.writeText "answers" ''
|
|
|
|
Task1:
|
|
|
|
${answer1}
|
|
|
|
|
|
|
|
Task2:
|
|
|
|
${answer2}
|
|
|
|
''
|