AoC-2022-Nix/3/solution.nix
zuckerberg 13f84a3296 Day 3
2022-12-03 19:46:12 -07:00

28 lines
850 B
Nix

with import ../util.nix;
let
# read input
input = readFile ./input;
bags = map (line: toPriority (stringToCharacters line)) (splitString "\n" input);
# converts item symbol to numerical priority
toPriority = l: map (c:
if c == toLower c then (charToInt c) - (charToInt "a") + 1
else (charToInt c) - (charToInt "A") + 27
) l;
# split bags into their two compartments
compartments = map (contents: cutInHalf contents) bags;
# find the item in common for the input bags/compartments
findCommonItem = l: head (intersectManyLists l);
# common items between the compartments per bag
commonItems = map findCommonItem compartments;
# common items between the bags per group of three elfs
commonItemsInElfGrp = map findCommonItem (chunksOf 3 bags);
in {
part1 = sum commonItems;
part2 = sum commonItemsInElfGrp;
}