Created
December 15, 2023 11:30
-
-
Save haakonvt/7cca951bd2414220172ba3f7f69ba19a to your computer and use it in GitHub Desktop.
AOC 2023, day 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from functools import reduce | |
from itertools import count, repeat | |
def reindeer(s): | |
return reduce(lambda i,c: (17*(i+ord(c)))%256, s, 0) | |
# Solution, part 1: | |
sum(map(reindeer, inp.split(","))) | |
boxes = [{} for _ in range(256)] | |
for cmd in inp.split(","): | |
if cmd.endswith("-"): | |
idx = reindeer((label := cmd[:-1])) | |
boxes[idx].pop(label, None) | |
else: | |
label, fc = cmd.split("=") | |
boxes[reindeer(label)][label] = int(fc) | |
# Solution, part 2: | |
sum(sum(map(math.prod, zip(boxes[i].values(), count(1), repeat(i+1)))) for i, b in enumerate(boxes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🎄