Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- player1, player2 = open("input.txt").read().strip().split("\n\n")
- player1, player2 = player1.split("\n")[1:], player2.split("\n")[1:]
- print(player1, player2)
- mem = {}
- def game(player1, player2, j):
- prev = {}
- i = 1
- subgamecount = 1
- print("Game %d" % j)
- while(len(player1) and len(player2)):
- str1 = ' '.join(player1)
- str2 = ' '.join(player2)
- if (str1,str2) in prev:
- return True
- #print("Game %d Round %d" % (j, i), str1, " :", str2)
- i += 1
- p1 = int(player1.pop(0))
- p2 = int(player2.pop(0))
- player1wins = None
- if (str1, str2) in mem:
- player1wins = mem[str1, str2]
- elif len(player1) >= p1 and len(player2) >= p2:
- player1wins = game(player1.copy()[:p1], player2.copy()[:p2], j + subgamecount)
- mem[str1, str2] = player1wins
- mem[str2, str1] = not player1wins
- subgamecount += 1
- else:
- player1wins = p1 > p2
- mem[str1, str2] = player1wins
- mem[str2, str1] = not player1wins
- if player1wins:
- player1.append(str(p1))
- player1.append(str(p2))
- else:
- player2.append(str(p2))
- player2.append(str(p1))
- prev[(str1, str2)] = player1wins
- return len(player1) > len(player2)
- player1wins = game(player1, player2, 1)
- player = player1 if player1wins else player2
- print(player1, player2)
- print(sum(int(player[i]) * (len(player) - i) for i in range(0, len(player))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement