Solve 2021 day 20, 21, 23, 25
This commit is contained in:
65
2021/d21.py
Normal file
65
2021/d21.py
Normal file
@@ -0,0 +1,65 @@
|
||||
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))
|
||||
Reference in New Issue
Block a user