work on day7, not complete
This commit is contained in:
parent
d921354b2a
commit
240f78775e
1000
2023/full/day07.txt
Normal file
1000
2023/full/day07.txt
Normal file
File diff suppressed because it is too large
Load Diff
83
2023/python/day07.py
Normal file
83
2023/python/day07.py
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
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()
|
5
2023/samples/day07.txt
Normal file
5
2023/samples/day07.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
32T3K 765
|
||||||
|
T55J5 684
|
||||||
|
KK677 28
|
||||||
|
KTJJT 220
|
||||||
|
QQQJA 483
|
Loading…
Reference in New Issue
Block a user