Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def part2(lines):
- hands = []
- result = 0
- for line in lines:
- string_pair = line.split()
- hands.append((string_pair[0], int(string_pair[1])))
- sorted_hands = sorted(hands, key=custom_sort_joker)
- sorted_hands.reverse()
- print(str(sorted_hands))
- for index,hand in enumerate(sorted_hands):
- result += hand[1] * (index+1)
- return result
- def custom_sort_joker(tuple):
- string, _ = tuple
- char_count = {}
- char_order = "AKQT98765432J"
- for char in string:
- char_count[char] = char_count.get(char, 0) + 1
- # Adjust counts for Jokers
- joker_count = char_count.get('J', 0)
- if joker_count > 0:
- for key in char_count:
- if key != 'J':
- count_with_jokers = char_count[key] + joker_count
- char_count[key] = max(char_count[key], count_with_jokers)
- additional_sort = [char_order.find(char) for char in string]
- if 5 in char_count.values():
- return (1, additional_sort)
- elif 4 in char_count.values():
- return (2, additional_sort)
- elif 3 in char_count.values():
- for key in char_count:
- if char_count.get(key) == 3:
- char_count[key] = 0
- break
- if char_count.get('J',0) > 0:
- jokers = char_count.get('J')
- for key in char_count.keys():
- char_count[key] = char_count[key] - jokers
- if 2 in char_count.values():
- return (3, additional_sort)
- else:
- return (4, additional_sort)
- elif list(char_count.values()).count(2) == 2 and char_count.get('J',0) == 0:
- return (5, additional_sort)
- elif 2 in char_count.values():
- return (6, additional_sort)
- else:
- return (7, additional_sort)
Advertisement
Add Comment
Please, Sign In to add comment