Advertisement
hhoppe

Advent of code 2021 day 21

Dec 21st, 2021
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. def process2(s, part2=False):
  2.   lines = s.strip('\n').split('\n')
  3.   pos0, pos1 = int(lines[0][27:]) - 1, int(lines[1][27:]) - 1
  4.   die_sum_distribution = collections.Counter(
  5.       sum(die) for die in itertools.product([1, 2, 3], repeat=3))
  6.  
  7.   @functools.lru_cache(maxsize=None)
  8.   def compute(pos0, pos1, score0, score1):
  9.     wins0 = wins1 = 0
  10.     for total, count in die_sum_distribution.items():
  11.       new_pos0 = (pos0 + total) % 10
  12.       new_score0 = score0 + (new_pos0 + 1)
  13.       if new_score0 >= 21:
  14.         wins0 += count
  15.       else:
  16.         d_wins1, d_wins0 = compute(pos1, new_pos0, score1, new_score0)
  17.         wins0 += d_wins0 * count
  18.         wins1 += d_wins1 * count
  19.     return wins0, wins1
  20.  
  21.   return max(*compute(pos0, pos1, 0, 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement