Advertisement
bob_f

AOC2023D07.py

Dec 14th, 2023 (edited)
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1.  
  2. from collections import Counter
  3.  
  4. def get_puzzle_input(a_file_name: str) -> list[str]:
  5.     with open(a_file_name) as INFILE:
  6.         return [line.rstrip().split()[0] for line in INFILE if not line.startswith('#')]
  7.    
  8. def sort_cards(a_card: str) -> int:
  9.  
  10.     rank_map = {
  11.         'K' : 2, 'Q' : 3, 'T' : 4,
  12.         '9' : 5, '8' : 6, '7' : 7, '6' : 8,
  13.         '5' : 9, '4' : 10, '3' : 11, '2' : 12,
  14.         'J' : 13, 'A' : 14 }
  15.  
  16.     return rank_map[a_card]
  17.  
  18. def upgrade_hand(a_hand: str) -> str:
  19.  
  20.     hand_counter = Counter(a_hand)
  21.     hand_set = set(hand_counter.keys())
  22.     hand_set.discard('J')
  23.     ranked_hand_set = sorted(hand_set, key=sort_cards)
  24.  
  25.     if hand_counter['J'] == 5:
  26.         return 'KKKKK'
  27.  
  28.     if hand_counter['J'] == 4:
  29.         for k in hand_counter:
  30.             if k == 'J':
  31.                 pass
  32.             else:
  33.                 return a_hand.replace('J', k)
  34.  
  35.     if hand_counter['J'] == 3:
  36.         if len(ranked_hand_set) == 1:
  37.             return ranked_hand_set[0] * 5
  38.         else:
  39.             return a_hand.replace('J', ranked_hand_set[0])
  40.  
  41.     if hand_counter['J'] == 2:
  42.         if len(ranked_hand_set) == 1:
  43.             return ranked_hand_set[0] * 5
  44.         elif len(ranked_hand_set) == 2:
  45.             for k, v in hand_counter.items():
  46.                 if k == 'J':
  47.                     pass
  48.                 elif v == 2:
  49.                     return a_hand.replace('J', k)
  50.         else:
  51.             return a_hand.replace('J', ranked_hand_set[0])
  52.  
  53.     if hand_counter['J'] == 1:
  54.         if len(ranked_hand_set) == 4:
  55.             return a_hand.replace('J', ranked_hand_set[0])
  56.         elif len(ranked_hand_set) == 1:
  57.             return ranked_hand_set[0] * 5
  58.         elif len(ranked_hand_set) in [2, 3]:
  59.             pairs = []
  60.             three_of_a_kind = []
  61.  
  62.             for k, v in hand_counter.items():
  63.                 if v == 3:
  64.                     three_of_a_kind.append(k)
  65.                 elif v == 2:
  66.                     pairs.append(k)
  67.            
  68.             if len(three_of_a_kind) > 0:
  69.                 return a_hand.replace('J', three_of_a_kind[0])
  70.             elif len(pairs) > 0:
  71.                 return a_hand.replace('J', sorted(pairs, key=sort_cards)[0])
  72.             else:
  73.                 assert False, 'Why am I here?'
  74.  
  75.         else:
  76.             assert False, 'Why am I here?'
  77.  
  78.     # No J card, return original hand
  79.  
  80.     return a_hand
  81.  
  82. hands = get_puzzle_input('AOC2023\AOC2023D07.txt')    
  83. print(f'{hands=}')
  84.  
  85. for hand in hands:
  86.     hand_upgraded = upgrade_hand(hand)
  87.     print(f'{hand=}, {hand_upgraded=}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement