21_dice.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python3
  2. import sys
  3. from itertools import cycle
  4. from functools import lru_cache
  5. def move(pos, score, amount):
  6. pos = (pos + amount - 1) % 10 + 1
  7. return pos, score + pos
  8. def practice(a, b):
  9. players = [(a, 0), (b, 0)]
  10. die = cycle(range(1, 101))
  11. rolls = 0
  12. while True:
  13. for player, (pos, score) in enumerate(players):
  14. roll = next(die) + next(die) + next(die)
  15. rolls += 3
  16. pos, score = players[player] = move(pos, score, roll)
  17. if score >= 1000:
  18. otherpos, otherscore = players[1 - player]
  19. return rolls * otherscore
  20. def play(a, b):
  21. @lru_cache(maxsize=None)
  22. def wins(a, b):
  23. awins = bwins = 0
  24. for x in range(1, 4):
  25. for y in range(1, 4):
  26. for z in range(1, 4):
  27. pos, score = roll_a = move(*a, x + y + z)
  28. if score >= 21:
  29. awins += 1
  30. else:
  31. roll_bwins, roll_awins = wins(b, roll_a)
  32. awins += roll_awins
  33. bwins += roll_bwins
  34. return awins, bwins
  35. return max(wins((a, 0), (b, 0)))
  36. a, b = (int(line.split(': ')[-1]) for line in sys.stdin)
  37. print(practice(a, b))
  38. print(play(a, b))