Files
aocpy/2021/d21.py
2024-11-27 17:31:52 -05:00

66 lines
1.5 KiB
Python

from lib import get_data
from lib import ints
from collections import defaultdict
FIELD = 10
DICE = 100
dice = -1
data = get_data(__file__)
_, p1, _, p2 = ints(data)
pos = [p1 - 1, p2 - 1]
scores = [0, 0]
dice_rolls = 0
not_done = True
while not_done:
for p in range(len(scores)):
dice_rolls += 3
for _ in range(3):
dice += 1 % DICE
pos[p] = (pos[p] + (dice + 1)) % FIELD
scores[p] += pos[p] + 1
if scores[p] >= 1000:
not_done = False
break
r = min(scores) * dice_rolls
print(r)
_, p1, _, p2 = ints(data)
pos = (p1 - 1, p2 - 1)
scores = (0, 0)
unis = defaultdict(int)
won = [0, 0]
unis[(0, pos, scores)] += 1
while unis:
nunis = defaultdict(int)
for (turn, opos, scores), count in unis.items():
other = (turn + 1) % 2
poss = [opos[turn]]
for _ in range(3):
nposs = []
for dice in range(1, 3 + 1):
for pos in poss:
npos = (pos + dice) % FIELD
nposs.append(npos)
poss = nposs
for npos_player in poss:
score = scores[turn] + npos_player + 1
if score >= 21:
won[turn] += count
else:
nscores = list(scores)
nscores[turn] = score
npos = list(opos)
npos[turn] = npos_player
state = (other, tuple(npos), tuple(nscores))
nunis[state] += count
unis = nunis
print(max(won))