main.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import os, sys
  2. task_dir = os.path.dirname(__file__)
  3. sys.path.append(f"{task_dir}/..")
  4. from get_tasks import get_input, generate_readme, check_example, bench
  5. from itertools import product
  6. from functools import cache
  7. from collections import Counter
  8. def virgin_warmup_game(player1=0, player2=0, pos1=4, pos2=8):
  9. roll = 1
  10. step = 0
  11. while not (player1 >= 1000 or player2 >= 1000):
  12. if step % 2 == 0:
  13. pos1 = 10 if (s := (sum(range(roll, roll + 3)) + pos1) % 10) == 0 else s
  14. player1 += pos1
  15. else:
  16. pos2 = 10 if (s := (sum(range(roll, roll + 3)) + pos2) % 10) == 0 else s
  17. player2 += pos2
  18. roll += 3
  19. step += 3
  20. roll = roll % 100 if roll > 100 else roll
  21. return min(player1, player2) * step
  22. outcomes = Counter(map(sum, product([1, 2, 3], [1, 2, 3], [1, 2, 3])))
  23. @cache
  24. def chad_dirac_dice(player1=0, player2=0, pos1=4, pos2=8):
  25. wins1, wins2 = 0, 0
  26. if player1 >= 21:
  27. return 1, 0
  28. if player2 >= 21:
  29. return 0, 1
  30. for s, c in outcomes.items():
  31. npos1 = 10 if (np := (s + pos1) % 10) == 0 else np
  32. a, b = chad_dirac_dice(player2, player1 + npos1, pos2, npos1)
  33. wins1 += b*c
  34. wins2 += a*c
  35. return wins1, wins2
  36. @bench
  37. def part1(input):
  38. pos1 = int(input[0][-1])
  39. pos2 = int(input[1][-1])
  40. print("The answer of part1 is:", virgin_warmup_game(pos1=pos1, pos2=pos2))
  41. @bench
  42. def part2(input):
  43. pos1 = int(input[0][-1])
  44. pos2 = int(input[1][-1])
  45. print("The answer of part2 is:", max(chad_dirac_dice(pos1=pos1, pos2=pos2)))
  46. if __name__ == "__main__":
  47. input, example = get_input(task_dir, 21)
  48. check_example(example, part1)
  49. check_example(example, part2)
  50. part1(input)
  51. part2(input)
  52. generate_readme(task_dir, 21)