fork download
  1. from collections import deque
  2.  
  3. def get_card_value(val):
  4. """Convert face cards to numbers."""
  5. if val == 'A': return 1
  6. if val == 'J': return 11
  7. if val == 'Q': return 12
  8. if val == 'K': return 13
  9. return int(val)
  10.  
  11. def rearrange_hand(hand, suit_priority):
  12. """Sort hand by value, then by suit priority."""
  13. return sorted(hand, key=lambda x: (x[0], suit_priority.index(x[1])))
  14.  
  15. def solve_game():
  16. try:
  17. N = int(input().strip())
  18.  
  19. p1, p2 = [], []
  20.  
  21. for _ in range(N):
  22. parts = input().strip().split()
  23. while len(parts) < 4: # handle empty lines
  24. parts = input().strip().split()
  25. C1, S1, C2, S2 = parts
  26. p1.append((get_card_value(C1), int(S1)))
  27. p2.append((get_card_value(C2), int(S2)))
  28.  
  29. # Read suit priority line safely
  30. while True:
  31. line = input().strip()
  32. if line:
  33. suit_priority = list(map(int, line.split()))
  34. break
  35.  
  36. # Initial arrangement
  37. p1 = deque(rearrange_hand(p1, suit_priority))
  38. p2 = deque(rearrange_hand(p2, suit_priority))
  39.  
  40. hand = []
  41. turn = 0 # 0 = Vaishnavi, 1 = Suraj
  42.  
  43. while p1 and p2:
  44. card = p1.popleft() if turn == 0 else p2.popleft()
  45.  
  46. if hand:
  47. top = hand[-1]
  48. if card[0] == top[0]:
  49. if suit_priority.index(card[1]) < suit_priority.index(top[1]):
  50. hand.append(card)
  51. hand = rearrange_hand(hand, suit_priority)
  52. if turn == 0:
  53. p1.extend(hand)
  54. else:
  55. p2.extend(hand)
  56. hand.clear()
  57. continue # same player continues
  58.  
  59. hand.append(card)
  60. turn = 1 - turn # alternate turns
  61.  
  62. # Determine result
  63. if len(p1) > 0 and len(p2) == 0:
  64. print("WINNER")
  65. elif len(p2) > 0 and len(p1) == 0:
  66. print("LOSER")
  67. else:
  68. print("TIE")
  69.  
  70. except Exception as e:
  71. print("Error:", e)
  72.  
  73. # Run
  74. solve_game()
  75.  
Success #stdin #stdout 0.09s 14004KB
stdin
5

K 2 Q 4

A 4 4 1

2 2 Q 1

4 2 3 1

5 1 5 2

3 2 1 4
stdout
WINNER