part1 done
This commit is contained in:
parent
240f78775e
commit
bd492c199e
@ -3,8 +3,12 @@ import shared
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
#STRENGTHS = list(reversed("A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2".split(", ")))
|
||||||
|
STRENGTHS = "A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2".split(", ")
|
||||||
|
STRENGTHS_TO_WEIGHT = {k:idx for idx, k in enumerate(STRENGTHS)}
|
||||||
|
|
||||||
STRENGTHS = list(reversed("A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2".split(", ")))
|
|
||||||
STRENGTH_HANDS = [
|
STRENGTH_HANDS = [
|
||||||
"Five of a Kind",
|
"Five of a Kind",
|
||||||
"Four of a Kind",
|
"Four of a Kind",
|
||||||
@ -48,6 +52,11 @@ class Hand:
|
|||||||
cards = [c for c in self.cards]
|
cards = [c for c in self.cards]
|
||||||
return determine_hand(self.cards)
|
return determine_hand(self.cards)
|
||||||
|
|
||||||
|
def sort_cards(self):
|
||||||
|
return [STRENGTHS_TO_WEIGHT[x] for x in self.cards]
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
return self.sort_cards() > other.sort_cards()
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
# @shared.profile
|
||||||
@ -57,9 +66,15 @@ def part1(rows):
|
|||||||
cards, bid = row.split()
|
cards, bid = row.split()
|
||||||
hand = Hand(cards=cards, bid=int(bid))
|
hand = Hand(cards=cards, bid=int(bid))
|
||||||
hands[hand.kind()].append(hand)
|
hands[hand.kind()].append(hand)
|
||||||
pprint(hands)
|
hands[hand.kind()] = list(sorted(hands[hand.kind()]))
|
||||||
|
in_order = []
|
||||||
|
for kind in reversed(STRENGTH_HANDS):
|
||||||
|
in_order.extend(hands[kind])
|
||||||
|
total = 0
|
||||||
|
for rank, card in enumerate(in_order):
|
||||||
|
total += (rank+1)*card.bid
|
||||||
|
|
||||||
|
print(total)
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
# @shared.profile
|
||||||
@ -73,7 +88,6 @@ def main():
|
|||||||
part1(rows)
|
part1(rows)
|
||||||
print("🕒", elapsed())
|
print("🕒", elapsed())
|
||||||
|
|
||||||
rows = [row for row in shared.load_rows(1,True)]
|
|
||||||
with shared.elapsed_timer() as elapsed:
|
with shared.elapsed_timer() as elapsed:
|
||||||
part2(rows)
|
part2(rows)
|
||||||
print("🕒", elapsed())
|
print("🕒", elapsed())
|
||||||
|
Loading…
Reference in New Issue
Block a user