hhoppe

Advent of code 2021 day 21 b

Dec 21st, 2021 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. def process2(s):  # Avoiding functools.lru_cache().
  2.   lines = s.strip('\n').split('\n')
  3.   pos = [int(lines[0][27:]), int(lines[1][27:])]
  4.   die_sum_distribution = collections.Counter(
  5.       sum(die) for die in itertools.product([1, 2, 3], repeat=3))
  6.   # Count of wins for [score0, score1, pos0, pos1, player]
  7.   wins = np.zeros((21, 21, 10, 10, 2), dtype=np.int64)
  8.  
  9.   for total_score in range(40, -1, -1):
  10.     for score0 in range(min(20, total_score), max(total_score - 21, -1), -1):
  11.       score1 = total_score - score0
  12.       for total, count in die_sum_distribution.items():
  13.         for pos0 in range(10):
  14.           new_pos0 = (pos0 + total) % 10
  15.           new_score0 = score0 + (new_pos0 + 1)
  16.           if new_score0 >= 21:
  17.             wins[score0, score1, pos0, :, 0] += count
  18.           else:
  19.             wins[score0, score1, pos0, :, :] += count * (
  20.                 wins[score1, new_score0, :, new_pos0, ::-1])
  21.  
  22.   return wins[0, 0, pos[0] - 1, pos[1] - 1].max()
Add Comment
Please, Sign In to add comment