|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- from collections import Counter
-
- from helpers import Helper
-
- helper = Helper(debug=True)
- debug = helper.debug
- load_input = helper.load_input
-
- CARD_VALUES = []
- for i in range(2,10):
- CARD_VALUES.append(str(i))
- CARD_VALUES.extend(["T", "J", "Q", "K", "A"])
-
- HAND_VALUES = [
- "High card",
- "One pair",
- "Two pair",
- "Three of a kind",
- "Full House",
- "Four of a kind",
- "Five of a kind"
- ]
-
- class Hand:
- def __init__(self, cards, bid):
- self.cards = cards
- self.bid = bid
-
- @property
- def value(self):
- card_counter = Counter(self.cards)
- card_counts = card_counter .most_common()
- if card_counts[0][1] == 5:
- return "Five of a kind"
- if card_counts[0][1] == 4:
- return "Four of a kind"
- if card_counts[0][1] == 3:
- if card_counts[1][1] == 2:
- return "Full House"
- return "Three of a kind"
- if card_counts[0][1] == 2:
- if card_counts[1][1] == 2:
- return "Two pair"
- return "One pair"
- return "High card"
-
- def __lt__(self, other):
- # They have different hand values
- if self.value != other.value:
- return HAND_VALUES.index(self.value) < HAND_VALUES.index(other.value)
- # They have the same hand value
- # So check each card in sequence
- for i in range(len(self.cards)):
- if self.cards[i] != other.cards[i]:
- # They have different nth cards
- return CARD_VALUES.index(self.cards[i]) < CARD_VALUES.index(other.cards[i])
- # They're the same
- return False
-
- def __str__(self):
- return f"Camel Cards hand: {self.cards} (value {self.value}, bid {self.bid})"
-
- def __repr__(self):
- return self.__str__()
-
-
- def main():
- lines = load_input(7)
- hands = []
- for line in lines:
- cards, bid = line.split()
- hands.append(Hand(
- cards = cards,
- bid = bid
- ))
- hands.sort()
- print(hands[:10])
-
- total_winnings = 0
- for i, hand in enumerate(hands):
- total_winnings += ((i+1) * int(hand.bid))
-
- print(f"Total winnings: {total_winnings}")
-
- if __name__ == "__main__":
- main()
|