Skip to content

Instantly share code, notes, and snippets.

@Solaxun
Created December 24, 2021 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Solaxun/6b9fc940078dceac704f00c7df66c770 to your computer and use it in GitHub Desktop.
Save Solaxun/6b9fc940078dceac704f00c7df66c770 to your computer and use it in GitHub Desktop.
day21.py
def mod1(num,m): return num % m or m
cache = {}
def play_round(player,pos1,score1,pos2,score2):
if (pos1,score1,pos2,score2) in cache:
return cache[(pos1,score1,pos2,score2)]
# swap 0/1 below to get the other player wins
if score1 >= 21:
return 1
if score2 >= 21:
return 0
wins = 0
for roll in itertools.product([1,2,3],repeat=3):
r = sum(roll)
if player == 1:
new_pos = points = mod1(pos1 + r,10)
wins += play_round(2,new_pos,score1+points,pos2,score2)
else:
new_pos = points = mod1(pos2 + r,10)
wins += play_round(1,pos1,score1,new_pos,score2+points)
cache[(pos1,score1,pos2,score2)] = wins
return wins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment