|
| 1 | +# Not working |
| 2 | +from enum import Enum |
| 3 | + |
| 4 | + |
| 5 | +class HandTypes(Enum): |
| 6 | + FIVE_KIND = 6 |
| 7 | + FOUR_KIND = 5 |
| 8 | + FULL_HOUSE = 4 |
| 9 | + THREE_KIND = 3 |
| 10 | + TWO_PAIR = 2 |
| 11 | + ONE_PAIR = 1 |
| 12 | + HIGH_CARD = 0 |
| 13 | + |
| 14 | + |
| 15 | +labels = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'] |
| 16 | + |
| 17 | + |
| 18 | +class Hand: |
| 19 | + def __init__(self, cards_str, bid_amount): |
| 20 | + self.cards_str: str = cards_str |
| 21 | + self.bid_amount: int = bid_amount |
| 22 | + self.rank: int or None = None |
| 23 | + |
| 24 | + # Determine hand type |
| 25 | + count_of_first_label = self.cards_str.count(self.cards_str[0]) |
| 26 | + |
| 27 | + if count_of_first_label == 5: |
| 28 | + self.type = HandTypes.FIVE_KIND |
| 29 | + elif count_of_first_label == 4: |
| 30 | + self.type = HandTypes.FOUR_KIND |
| 31 | + else: |
| 32 | + self.type = determine_hands_type(cards_str, count_of_first_label) |
| 33 | + print(self.type) |
| 34 | + |
| 35 | + # To be honest, this method is not really necessary |
| 36 | + def __eq__(self, other): |
| 37 | + if self.type == other.type and self.cards_str == other.cards_str: |
| 38 | + return True |
| 39 | + |
| 40 | + def __lt__(self, other): |
| 41 | + if self.type.value < other.type.value: |
| 42 | + return True |
| 43 | + |
| 44 | + if self.type == other.type: |
| 45 | + for i in range(0, 5): |
| 46 | + self_label_value = labels.index(self.cards_str[i]) |
| 47 | + other_label_value = labels.index(other.cards_str[i]) |
| 48 | + |
| 49 | + if self_label_value < other_label_value: |
| 50 | + return True |
| 51 | + |
| 52 | + return False |
| 53 | + |
| 54 | + # To be honest, this method is also not really necessary |
| 55 | + def __gt__(self, other): |
| 56 | + if self.type.value > other.type.value: |
| 57 | + return True |
| 58 | + |
| 59 | + if self.type == other.type: |
| 60 | + for i in range(0, 5): |
| 61 | + self_label_value = labels.index(self.cards_str[i]) |
| 62 | + other_label_value = labels.index(other.cards_str[i]) |
| 63 | + |
| 64 | + if self_label_value > other_label_value: |
| 65 | + return True |
| 66 | + |
| 67 | + return False |
| 68 | + |
| 69 | + |
| 70 | +def determine_hands_type(cards_str: str, last_found_count: int, pair_found: bool = False): |
| 71 | + label = cards_str[0] |
| 72 | + label_count = cards_str.count(label) |
| 73 | + if last_found_count == 3: |
| 74 | + if label_count == 2: |
| 75 | + return HandTypes.FULL_HOUSE |
| 76 | + else: |
| 77 | + return HandTypes.THREE_KIND |
| 78 | + if last_found_count == 2: |
| 79 | + if label_count == 2: |
| 80 | + return HandTypes.TWO_PAIR |
| 81 | + else: |
| 82 | + determine_hands_type(cards_str.replace(label, ""), label_count, True) |
| 83 | + if last_found_count == 1: |
| 84 | + if pair_found and label_count == 2: |
| 85 | + return HandTypes.TWO_PAIR |
| 86 | + if label_count == 2: |
| 87 | + if len(cards_str) > 3: |
| 88 | + return determine_hands_type(cards_str.replace(label, ""), label_count, True) |
| 89 | + else: |
| 90 | + return HandTypes.ONE_PAIR |
| 91 | + if label_count == 1: |
| 92 | + if len(cards_str) > 3: |
| 93 | + return determine_hands_type(cards_str.replace(label, ""), label_count, pair_found) |
| 94 | + else: |
| 95 | + return HandTypes.HIGH_CARD |
| 96 | + |
| 97 | + |
| 98 | +def main(): |
| 99 | + hands = [] |
| 100 | + total_winning = 0 |
| 101 | + with open("day7_input.txt", "r") as input_file: |
| 102 | + hand_strs = input_file.readlines() |
| 103 | + |
| 104 | + for hand_idx, hand_str in enumerate(hand_strs): |
| 105 | + [cards_str, bid_amount] = hand_str.split(" ") |
| 106 | + hands.append(Hand(cards_str, int(bid_amount))) |
| 107 | + |
| 108 | + hands.sort() |
| 109 | + |
| 110 | + for rank, hand in enumerate(hands): |
| 111 | + total_winning += rank * hand.bid_amount |
| 112 | + |
| 113 | + print("Total Winning:", total_winning) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + main() |
0 commit comments