Advertisement
Guest User

Advent of Code 2021 Day 21

a guest
Dec 21st, 2021
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1.  
  2. from functools import cache
  3.  
  4.  
  5. @cache
  6. def play_dice(
  7.     p1_position,
  8.     p2_position,
  9.     dice_roll_n=0,
  10.     dice_sum=0,
  11.     p1_score=0,
  12.     p2_score=0,
  13.     next_player=1,
  14.     winning_score=21,
  15.     board_size=10,
  16. ):
  17.     p1_wins = 0
  18.     p2_wins = 0
  19.     if next_player == 1:
  20.         if dice_roll_n < 3:
  21.             for i in range(1, 4):
  22.                 next_dice_sum = dice_sum + i
  23.                 next_dice_roll_n = dice_roll_n + 1
  24.                 wins = play_dice(
  25.                     p1_position,
  26.                     p2_position,
  27.                     next_dice_roll_n,
  28.                     next_dice_sum,
  29.                     p1_score,
  30.                     p2_score,
  31.                     next_player,
  32.                 )
  33.                 p1_wins += wins[0]
  34.                 p2_wins += wins[1]
  35.         else:
  36.             dice_roll_n = 0
  37.             next_p1_position = p1_position + dice_sum
  38.             if next_p1_position > board_size:
  39.                 next_p1_position %= board_size
  40.                 if next_p1_position == 0:
  41.                     next_p1_position = board_size
  42.             next_p1_score = p1_score + next_p1_position
  43.             if next_p1_score >= winning_score:
  44.                 p1_wins += 1
  45.             else:
  46.                 wins = play_dice(
  47.                     next_p1_position,
  48.                     p2_position,
  49.                     dice_roll_n,
  50.                     dice_sum=0,
  51.                     p1_score=next_p1_score,
  52.                     p2_score=p2_score,
  53.                     next_player=2,
  54.                 )
  55.                 p1_wins += wins[0]
  56.                 p2_wins += wins[1]
  57.  
  58.     if next_player == 2:
  59.         if dice_roll_n < 3:
  60.             for i in range(1, 4):
  61.                 next_dice_sum = dice_sum + i
  62.                 next_dice_roll_n = dice_roll_n + 1
  63.                 wins = play_dice(
  64.                     p1_position,
  65.                     p2_position,
  66.                     next_dice_roll_n,
  67.                     next_dice_sum,
  68.                     p1_score,
  69.                     p2_score,
  70.                     next_player,
  71.                 )
  72.                 p1_wins += wins[0]
  73.                 p2_wins += wins[1]
  74.         else:
  75.             dice_roll_n = 0
  76.             next_p2_position = p2_position + dice_sum
  77.             if next_p2_position > board_size:
  78.                 next_p2_position %= board_size
  79.                 if next_p2_position == 0:
  80.                     next_p2_position = board_size
  81.             next_p2_score = p2_score + next_p2_position
  82.             if next_p2_score >= winning_score:
  83.                 p2_wins += 1
  84.             else:
  85.                 wins = play_dice(
  86.                     p1_position,
  87.                     next_p2_position,
  88.                     dice_roll_n,
  89.                     dice_sum=0,
  90.                     p1_score=p1_score,
  91.                     p2_score=next_p2_score,
  92.                     next_player=1,
  93.                 )
  94.                 p1_wins += wins[0]
  95.                 p2_wins += wins[1]
  96.  
  97.     return p1_wins, p2_wins
  98.  
  99.  
  100. p1_pos = 4
  101. p2_pos = 8
  102. p1_wins, p2_wins = play_dice(p1_pos, p2_pos)
  103. print(max(p1_wins, p2_wins))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement