Created
December 24, 2021 20:13
-
-
Save Solaxun/6b9fc940078dceac704f00c7df66c770 to your computer and use it in GitHub Desktop.
day21.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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