Guest User

Untitled

a guest
Dec 7th, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. def part2(lines):
  2. hands = []
  3. result = 0
  4. for line in lines:
  5. string_pair = line.split()
  6. hands.append((string_pair[0], int(string_pair[1])))
  7. sorted_hands = sorted(hands, key=custom_sort_joker)
  8. sorted_hands.reverse()
  9. print(str(sorted_hands))
  10. for index,hand in enumerate(sorted_hands):
  11. result += hand[1] * (index+1)
  12. return result
  13.  
  14.  
  15. def custom_sort_joker(tuple):
  16. string, _ = tuple
  17. char_count = {}
  18. char_order = "AKQT98765432J"
  19. for char in string:
  20. char_count[char] = char_count.get(char, 0) + 1
  21.  
  22. # Adjust counts for Jokers
  23. joker_count = char_count.get('J', 0)
  24. if joker_count > 0:
  25. for key in char_count:
  26. if key != 'J':
  27. count_with_jokers = char_count[key] + joker_count
  28. char_count[key] = max(char_count[key], count_with_jokers)
  29. additional_sort = [char_order.find(char) for char in string]
  30. if 5 in char_count.values():
  31. return (1, additional_sort)
  32. elif 4 in char_count.values():
  33. return (2, additional_sort)
  34. elif 3 in char_count.values():
  35. for key in char_count:
  36. if char_count.get(key) == 3:
  37. char_count[key] = 0
  38. break
  39. if char_count.get('J',0) > 0:
  40. jokers = char_count.get('J')
  41. for key in char_count.keys():
  42. char_count[key] = char_count[key] - jokers
  43. if 2 in char_count.values():
  44. return (3, additional_sort)
  45. else:
  46. return (4, additional_sort)
  47. elif list(char_count.values()).count(2) == 2 and char_count.get('J',0) == 0:
  48. return (5, additional_sort)
  49. elif 2 in char_count.values():
  50. return (6, additional_sort)
  51. else:
  52. return (7, additional_sort)
Advertisement
Add Comment
Please, Sign In to add comment