You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
11 months ago
|
ORDERING = "123456789TJQKA"
|
||
|
|
||
|
|
||
|
def do_task(infile):
|
||
|
hands = [(line.split()[0], int(line.split()[1])) for line in infile.split("\n")]
|
||
|
|
||
|
hands_parsed = []
|
||
|
|
||
|
for hand, value in hands:
|
||
|
cards = dict(zip("123456789TJQKA", [0] * len(ORDERING)))
|
||
|
for card in hand:
|
||
|
cards[card] += 1
|
||
|
hands_parsed.append((hand, value, cards))
|
||
|
|
||
|
hands_parsed.sort(key=evaluate_hand)
|
||
|
|
||
|
score_sum = 0
|
||
|
|
||
|
for rank, (_, score, _) in enumerate(hands_parsed, start=1):
|
||
|
score_sum += rank * score
|
||
|
|
||
|
return score_sum
|
||
|
|
||
|
|
||
|
def evaluate_hand(h):
|
||
|
(hand, _, cards) = h
|
||
|
|
||
|
cv = max(cards.values()) << 4
|
||
|
|
||
|
if cv == 0x30:
|
||
|
if 2 in cards.values():
|
||
|
cv += 1
|
||
|
|
||
|
if cv == 0x20:
|
||
|
pair_count = 0
|
||
|
for v in cards.values():
|
||
|
if v == 2:
|
||
|
pair_count += 1
|
||
|
cv += 1 if pair_count == 2 else 0
|
||
|
|
||
|
for card in hand:
|
||
|
cv <<= 4
|
||
|
cv += ORDERING.index(card)
|
||
|
|
||
|
return cv
|
||
|
|
||
|
|
||
|
with open("day07/data/input100.txt") as infile:
|
||
|
result = do_task(infile.read())
|
||
|
print(result)
|
||
|
print(result == 6440)
|
||
|
|
||
|
with open("day07/data/input101.txt") as infile:
|
||
|
result = do_task(infile.read())
|
||
|
print(result)
|
||
|
print(result == 256448566)
|