84 lines
1.7 KiB
Python
84 lines
1.7 KiB
Python
|
import matrix
|
||
|
import shared
|
||
|
from dataclasses import dataclass
|
||
|
from collections import defaultdict
|
||
|
from pprint import pprint
|
||
|
|
||
|
STRENGTHS = list(reversed("A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2".split(", ")))
|
||
|
STRENGTH_HANDS = [
|
||
|
"Five of a Kind",
|
||
|
"Four of a Kind",
|
||
|
"Full House",
|
||
|
"Three of a Kind",
|
||
|
"Two Pair",
|
||
|
"One Pair",
|
||
|
"High Card",
|
||
|
]
|
||
|
|
||
|
def determine_hand(cards):
|
||
|
card_set = set(cards)
|
||
|
if len(card_set) == 1:
|
||
|
return "Five of a Kind"
|
||
|
|
||
|
|
||
|
same_cards = sorted([cards.count(a) for a in card_set])
|
||
|
if len(same_cards) == 2:
|
||
|
if max(same_cards) == 4:
|
||
|
return "Four of a Kind"
|
||
|
elif max(same_cards) == 3:
|
||
|
return "Full House"
|
||
|
elif len(same_cards) == 3:
|
||
|
if max(same_cards) == 3:
|
||
|
return "Three of a Kind"
|
||
|
else:
|
||
|
return "Two Pair"
|
||
|
elif len(same_cards) == 4:
|
||
|
return "One Pair"
|
||
|
return "High Card"
|
||
|
|
||
|
@dataclass
|
||
|
class Hand:
|
||
|
cards: str
|
||
|
bid: int
|
||
|
|
||
|
def counts(self):
|
||
|
return Counter(self.cards)
|
||
|
|
||
|
def kind(self):
|
||
|
cards = [c for c in self.cards]
|
||
|
return determine_hand(self.cards)
|
||
|
|
||
|
|
||
|
|
||
|
# @shared.profile
|
||
|
def part1(rows):
|
||
|
hands = defaultdict(list)
|
||
|
for row in rows:
|
||
|
cards, bid = row.split()
|
||
|
hand = Hand(cards=cards, bid=int(bid))
|
||
|
hands[hand.kind()].append(hand)
|
||
|
pprint(hands)
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
# @shared.profile
|
||
|
def part2(rows):
|
||
|
pass
|
||
|
|
||
|
|
||
|
def main():
|
||
|
rows = [row for row in shared.load_rows(7)]
|
||
|
with shared.elapsed_timer() as elapsed:
|
||
|
part1(rows)
|
||
|
print("🕒", elapsed())
|
||
|
|
||
|
rows = [row for row in shared.load_rows(1,True)]
|
||
|
with shared.elapsed_timer() as elapsed:
|
||
|
part2(rows)
|
||
|
print("🕒", elapsed())
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|