Advertisement
icarussiano

day 7(part 2)

Dec 7th, 2023 (edited)
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. with open("input") as file:
  2.     hands = [(line.split()[0],int(line.split()[1])) for line in file.read().splitlines()]
  3.  
  4. def hand_type2(hand):
  5.     counts = [hand.count(card) for card in 'AKQT98765432J']
  6.     if (max(counts[:-1])+counts[-1]==5):
  7.         return 6
  8.     elif (max(counts[:-1])+counts[-1]==4):
  9.         return 5
  10.     elif (3 in counts and 2 in counts) or (counts.count(2) == 2 and 'J' in hand):
  11.         return 4
  12.     elif (max(counts[:-1])+counts[-1]==3):
  13.         return 3
  14.     elif counts.count(2) == 2 or (counts.count(2) == 1 and 'J' in hand):
  15.         return 2
  16.     elif 2 in counts or 'J' in hand:
  17.         return 1
  18.     return 0
  19.  
  20. hands2 = sorted(sorted(hands, key=lambda x: ['J23456789TQKA'.index(c) for c in x[0]]), key=lambda x: hand_type2(x[0]))
  21. print(sum([hand[1]*(i+1) for i, hand in enumerate(hands2)]))
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement