Advertisement
Guest User

22p2

a guest
Dec 22nd, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. player1, player2 = open("input.txt").read().strip().split("\n\n")
  2.  
  3. player1, player2 = player1.split("\n")[1:], player2.split("\n")[1:]
  4. print(player1, player2)
  5.  
  6. mem = {}
  7.  
  8. def game(player1, player2, j):
  9.     prev = {}
  10.     i = 1
  11.     subgamecount = 1
  12.     print("Game %d" % j)
  13.     while(len(player1) and len(player2)):
  14.         str1 = ' '.join(player1)
  15.         str2 = ' '.join(player2)
  16.         if (str1,str2) in prev:
  17.             return True
  18.         #print("Game %d Round %d" % (j, i), str1, " :", str2)
  19.         i += 1
  20.         p1 = int(player1.pop(0))
  21.         p2 = int(player2.pop(0))
  22.        
  23.         player1wins = None
  24.         if (str1, str2) in mem:
  25.             player1wins = mem[str1, str2]
  26.         elif len(player1) >= p1 and len(player2) >= p2:
  27.             player1wins = game(player1.copy()[:p1], player2.copy()[:p2], j + subgamecount)
  28.             mem[str1, str2] = player1wins
  29.             mem[str2, str1] = not player1wins
  30.             subgamecount += 1
  31.         else:
  32.             player1wins = p1 > p2
  33.             mem[str1, str2] = player1wins
  34.             mem[str2, str1] = not player1wins
  35.         if player1wins:        
  36.             player1.append(str(p1))
  37.             player1.append(str(p2))
  38.         else:
  39.             player2.append(str(p2))
  40.             player2.append(str(p1))
  41.        
  42.         prev[(str1, str2)] = player1wins
  43.     return len(player1) > len(player2)
  44.    
  45.  
  46.  
  47.  
  48. player1wins = game(player1, player2, 1)
  49. player = player1 if player1wins else player2
  50. print(player1, player2)
  51.  
  52.  
  53. 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