Advertisement
hhoppe

Advent of code 2023 day 7

Dec 6th, 2023 (edited)
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. def day7(s, *, part2=False):
  2.   rank = ['23456789TJQKA', 'J23456789TQKA'][part2]
  3.  
  4.   def order(hand):
  5.     non_jokers = [card for card in hand if not (card == 'J' and part2)]
  6.     counts = sorted(collections.Counter(non_jokers).values())[::-1] or [0]
  7.     counts[0] += len(hand) - len(non_jokers)
  8.     return counts, [rank.index(card) for card in hand]
  9.  
  10.   hand_bids = sorted((line.split() for line in s.splitlines()), key=lambda t: order(t[0]))
  11.   return sum(int(bid) * i for i, (_, bid) in enumerate(hand_bids, 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement