Selaa lähdekoodia

guestroom

master
Roderic Day 3 vuotta sitten
vanhempi
commit
1e87eb795b
1 muutettua tiedostoa jossa 30 lisäystä ja 0 poistoa
  1. +30
    -0
      y2021/p21.py

+ 30
- 0
y2021/p21.py Näytä tiedosto

@@ -0,0 +1,30 @@
from functools import lru_cache
from itertools import product, cycle, count


@lru_cache(maxsize=None)
def play(p1, p2, s1=0, s2=0):
if s2 >= 21: return 0, 1
w1, w2 = 0, 0
for die in d3:
pN = (p1 + die) % 10 or 10
n2, n1 = play(p2, pN, s2, s1 + pN)
w1, w2 = (w1 + n1), (w2 + n2)
return w1, w2


text = open(0).read()
d3 = [sum(rolls) for rolls in product(range(1, 4), repeat=3)]
p1, p2 = [int(ln[-1]) for ln in text.splitlines()]

state = {0: (0, p1), 1: (0, p2)}
d100 = cycle(range(1,101))
for i in count():
score, pos = state[i % 2]
pos = (pos + next(d100) + next(d100) + next(d100)) % 10 or 10
state[i % 2] = (score + pos, pos)
if score + pos >= 1000:
break
print((i + 1) * 3 * min(state.values())[0])

print(max(play(p1, p2)))

Loading…
Peruuta
Tallenna