Skip to content

Instantly share code, notes, and snippets.

@Mu-adventofcode
Last active July 2, 2022 17:01
Show Gist options
  • Save Mu-adventofcode/0cfa6fe66302641a6d4b1bbb0553a6c9 to your computer and use it in GitHub Desktop.
Save Mu-adventofcode/0cfa6fe66302641a6d4b1bbb0553a6c9 to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 21 part 2
import re
from collections import defaultdict
DISPATCHES = {3: 1, 4: 3, 5: 6, 6: 7, 7: 6, 8: 3, 9: 1}
inp = open("input_21.txt").read().strip()
pos = tuple(map(lambda x: int(x) - 1, re.findall(r": (\d+)", inp)))
ucnts = defaultdict(int)
ucnts[((pos[0], 0), (pos[1], 0))] = 1 # key: ((pos, score), (pos, score))
wins = [0, 0]
pid = 0 # player id
while ucnts:
tmp = defaultdict(int)
for key, cnt in ucnts.items():
pos, score = key[pid]
for delta, n in DISPATCHES.items():
newpos = (pos + delta) % 10
newscore = score + newpos + 1
newcnt = cnt * n
if newscore >= 21:
wins[pid] += newcnt
else:
if pid == 0:
tmp[((newpos, newscore), key[1])] += newcnt
else:
tmp[(key[0], (newpos, newscore))] += newcnt
ucnts = tmp
pid = 1 - pid
print(max(wins))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment