Skip to content

Instantly share code, notes, and snippets.

@haakonvt
Created December 15, 2023 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haakonvt/7cca951bd2414220172ba3f7f69ba19a to your computer and use it in GitHub Desktop.
Save haakonvt/7cca951bd2414220172ba3f7f69ba19a to your computer and use it in GitHub Desktop.
AOC 2023, day 15
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))
@kung-foo
Copy link

🎄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment