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; }