|
| 1 | +import functools |
| 2 | + |
| 3 | +# read input |
| 4 | +with open("input.txt") as f: |
| 5 | + input_list = f.read().splitlines() |
| 6 | +bids = {} |
| 7 | +hands = [] |
| 8 | +for line in input_list: |
| 9 | + cards, bid = line.split() |
| 10 | + bid = int(bid) |
| 11 | + bids[cards] = bid |
| 12 | + hands.append(cards) |
| 13 | + |
| 14 | +# custom sort |
| 15 | +""" |
| 16 | +If two hands have the same type, a second ordering rule takes effect. Start by comparing the first card in each hand. If these cards are different, the hand with the stronger first card is considered stronger. If the first card in each hand have the same label, however, then move on to considering the second card in each hand. If they differ, the hand with the higher second card wins; otherwise, continue with the third card in each hand, then the fourth, then the fifth. |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +def compare(hand1, hand2): |
| 21 | + for x in range(6): |
| 22 | + if values.index(hand1[x]) < values.index(hand2[x]): |
| 23 | + return -1 |
| 24 | + if values.index(hand1[x]) > values.index(hand2[x]): |
| 25 | + return 1 |
| 26 | + return 0 |
| 27 | + |
| 28 | + |
| 29 | +# sorted strongest -> lowest |
| 30 | +values = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"] |
| 31 | + |
| 32 | +ranks = [] |
| 33 | +five_kind = [] |
| 34 | +four_kind = [] |
| 35 | +full_house = [] |
| 36 | +three_one_one_pair = [] |
| 37 | +two_one_one_pair = [] |
| 38 | +one_one_pair = [] |
| 39 | +high_cards = [] |
| 40 | + |
| 41 | +for hand in hands: |
| 42 | + if len(set(list(hand))) == 1: |
| 43 | + five_kind.append(hand) |
| 44 | + if len(set(list(hand))) == 2: |
| 45 | + if hand.count(hand[0]) == 1 or hand.count(hand[0]) == 4: |
| 46 | + four_kind.append(hand) |
| 47 | + else: |
| 48 | + full_house.append(hand) |
| 49 | + if len(set(list(hand))) == 3: |
| 50 | + if ( |
| 51 | + hand.count(hand[0]) == 3 |
| 52 | + or hand.count(hand[1]) == 3 |
| 53 | + or hand.count(hand[2]) == 3 |
| 54 | + ): |
| 55 | + three_one_one_pair.append(hand) |
| 56 | + else: |
| 57 | + two_one_one_pair.append(hand) |
| 58 | + if len(set(list(hand))) == 4: |
| 59 | + one_one_pair.append(hand) |
| 60 | + if len(set(list(hand))) == 5: |
| 61 | + high_cards.append(hand) |
| 62 | + |
| 63 | +# secondary sort |
| 64 | +five_kind = sorted(five_kind, key=functools.cmp_to_key(compare)) |
| 65 | +four_kind = sorted(four_kind, key=functools.cmp_to_key(compare)) |
| 66 | +full_house = sorted(full_house, key=functools.cmp_to_key(compare)) |
| 67 | +three_one_one_pair = sorted(three_one_one_pair, key=functools.cmp_to_key(compare)) |
| 68 | +two_one_one_pair = sorted(two_one_one_pair, key=functools.cmp_to_key(compare)) |
| 69 | +one_one_pair = sorted(one_one_pair, key=functools.cmp_to_key(compare)) |
| 70 | +high_cards = sorted(high_cards, key=functools.cmp_to_key(compare)) |
| 71 | + |
| 72 | +# primary sort |
| 73 | +for hand in five_kind: |
| 74 | + ranks.append(hand) |
| 75 | +for hand in four_kind: |
| 76 | + ranks.append(hand) |
| 77 | +for hand in full_house: |
| 78 | + ranks.append(hand) |
| 79 | +for hand in three_one_one_pair: |
| 80 | + ranks.append(hand) |
| 81 | +for hand in two_one_one_pair: |
| 82 | + ranks.append(hand) |
| 83 | +for hand in one_one_pair: |
| 84 | + ranks.append(hand) |
| 85 | +for hand in high_cards: |
| 86 | + ranks.append(hand) |
| 87 | + |
| 88 | +i = 1 |
| 89 | +result = 0 |
| 90 | +for hand in reversed(ranks): |
| 91 | + result += i * bids[hand] |
| 92 | + i += 1 |
| 93 | + |
| 94 | +print("PART ONE:", result) |
| 95 | + |
| 96 | +ranks = [] |
| 97 | +five_kind = [] |
| 98 | +four_kind = [] |
| 99 | +full_house = [] |
| 100 | +three_kind = [] |
| 101 | +two_kind = [] |
| 102 | +one_pair = [] |
| 103 | +high_card = [] |
| 104 | + |
| 105 | +# PART 2 - adjust secondary rank (within kinds) so that J is weakest card |
| 106 | +values = ["A", "K", "Q", "T", "9", "8", "7", "6", "5", "4", "3", "2", "J"] |
| 107 | + |
| 108 | +for hand in hands: |
| 109 | + if len(set(list(hand))) == 1: |
| 110 | + five_kind.append(hand) |
| 111 | + if len(set(list(hand))) == 2: |
| 112 | + if "J" in hand: |
| 113 | + five_kind.append(hand) |
| 114 | + elif hand.count(hand[0]) == 1 or hand.count(hand[0]) == 4: |
| 115 | + four_kind.append(hand) |
| 116 | + else: |
| 117 | + full_house.append(hand) |
| 118 | + if len(set(list(hand))) == 3: |
| 119 | + if ( |
| 120 | + hand.count(hand[0]) == 3 |
| 121 | + or hand.count(hand[1]) == 3 |
| 122 | + or hand.count(hand[2]) == 3 |
| 123 | + ): |
| 124 | + if "J" in hand: |
| 125 | + four_kind.append(hand) |
| 126 | + else: |
| 127 | + three_kind.append(hand) |
| 128 | + else: |
| 129 | + if "J" in hand: |
| 130 | + if hand.count("J") == 2: |
| 131 | + four_kind.append(hand) |
| 132 | + else: |
| 133 | + full_house.append(hand) |
| 134 | + else: |
| 135 | + two_kind.append(hand) |
| 136 | + if len(set(list(hand))) == 4: |
| 137 | + if "J" in hand: |
| 138 | + three_kind.append(hand) |
| 139 | + else: |
| 140 | + one_pair.append(hand) |
| 141 | + if len(set(list(hand))) == 5: |
| 142 | + if "J" in hand: |
| 143 | + one_pair.append(hand) |
| 144 | + else: |
| 145 | + high_card.append(hand) |
| 146 | + |
| 147 | +five_kind = sorted(five_kind, key=functools.cmp_to_key(compare)) |
| 148 | +four_kind = sorted(four_kind, key=functools.cmp_to_key(compare)) |
| 149 | +full_house = sorted(full_house, key=functools.cmp_to_key(compare)) |
| 150 | +three_kind = sorted(three_kind, key=functools.cmp_to_key(compare)) |
| 151 | +two_kind = sorted(two_kind, key=functools.cmp_to_key(compare)) |
| 152 | +one_pair = sorted(one_pair, key=functools.cmp_to_key(compare)) |
| 153 | +high_card = sorted(high_card, key=functools.cmp_to_key(compare)) |
| 154 | + |
| 155 | +for hand in five_kind: |
| 156 | + ranks.append(hand) |
| 157 | +for hand in four_kind: |
| 158 | + ranks.append(hand) |
| 159 | +for hand in full_house: |
| 160 | + ranks.append(hand) |
| 161 | +for hand in three_kind: |
| 162 | + ranks.append(hand) |
| 163 | +for hand in two_kind: |
| 164 | + ranks.append(hand) |
| 165 | +for hand in one_pair: |
| 166 | + ranks.append(hand) |
| 167 | +for hand in high_card: |
| 168 | + ranks.append(hand) |
| 169 | + |
| 170 | +i = 1 |
| 171 | +answer2 = 0 |
| 172 | +for hand in reversed(ranks): |
| 173 | + answer2 += i * bids[hand] |
| 174 | + i += 1 |
| 175 | + |
| 176 | +print("Answer 2:", answer2) |
0 commit comments