advent_of_code_2023/Day07/python/solution1.py

96 lines
2.4 KiB
Python
Raw Normal View History

2023-12-07 15:31:49 +00:00
from collections import Counter
2023-12-10 16:24:14 +00:00
2023-12-07 15:31:49 +00:00
def read_hands(file_path):
"""
Reads hands and their bids from the file.
Returns a list of tuples (hand, bid).
"""
try:
2023-12-10 16:24:14 +00:00
with open(file_path, "r") as file:
2023-12-07 15:31:49 +00:00
return [line.strip().split() for line in file]
except Exception as e:
print(f"Error reading file {file_path}: {e}")
raise
2023-12-10 16:24:14 +00:00
2023-12-07 15:31:49 +00:00
def replace_face_cards(hand):
"""
Replaces face cards with characters that maintain the card order.
"""
2023-12-10 16:24:14 +00:00
replacements = {"T": "a", "J": "b", "Q": "c", "K": "d", "A": "e"}
return "".join(replacements.get(card, card) for card in hand)
2023-12-07 15:31:49 +00:00
def categorize_hand(hand):
"""
Categorizes the hand into its type and returns a tuple of (category, hand).
"""
hand = replace_face_cards(hand)
card_count = Counter(hand)
if 5 in card_count.values():
category = 7
elif 4 in card_count.values():
category = 6
elif 3 in card_count.values() and 2 in card_count.values():
category = 5
elif 3 in card_count.values():
category = 4
elif list(card_count.values()).count(2) == 2:
category = 3
elif 2 in card_count.values():
category = 2
else:
category = 1
return (category, hand)
2023-12-10 16:24:14 +00:00
2023-12-07 15:31:49 +00:00
def hand_key(hand):
"""
Key function for sorting hands.
Converts the hand into a format that can be compared directly.
"""
return categorize_hand(hand[0])
2023-12-10 16:24:14 +00:00
2023-12-07 15:31:49 +00:00
def calculate_total_winnings(file_path):
"""
Calculates the total winnings from the hands in the file.
"""
hands = read_hands(file_path)
sorted_hands = sorted(hands, key=hand_key, reverse=True)
2023-12-10 16:24:14 +00:00
total_winnings = sum(
int(bid) * (len(hands) - index) for index, (_, bid) in enumerate(sorted_hands)
)
2023-12-07 15:31:49 +00:00
return total_winnings
2023-12-10 16:24:14 +00:00
2023-12-07 15:31:49 +00:00
def test():
2023-12-10 16:24:14 +00:00
print("Running test...")
2023-12-07 15:31:49 +00:00
try:
expected_result = 6440
total_winnings = calculate_total_winnings("../test.txt")
2023-12-10 16:24:14 +00:00
assert (
total_winnings == expected_result
), f"Test failed, expected {expected_result} but got {total_winnings}"
2023-12-07 15:31:49 +00:00
print(f"Test passed: {total_winnings}")
except Exception as e:
print(f"Test failed: {e}")
2023-12-10 16:24:14 +00:00
2023-12-07 15:31:49 +00:00
def main():
try:
test()
result = calculate_total_winnings("../input.txt")
print(f"Total result from input.txt: {result}")
except Exception as e:
print(f"An error occurred: {e}")
2023-12-10 16:24:14 +00:00
2023-12-07 15:31:49 +00:00
if __name__ == "__main__":
main()