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.

59 lines
1.2 KiB
Python

ORDERING = "J123456789TQKA"
def do_task(infile):
hands = [(line.split()[0], int(line.split()[1])) for line in infile.split("\n")]
hands.sort(key=evaluate_hand)
score_sum = 0
for rank, (_, score) in enumerate(hands, start=1):
score_sum += rank * score
return score_sum
def evaluate_hand(h):
(hand, _) = h
cards = dict.fromkeys("123456789TQKA", 0)
j = 0
for card in hand:
if card != "J":
cards[card] += 1
else:
j += 1
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 += pair_count == 2
cv += j << 4
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 == 5905)
with open("day07/data/input101.txt") as infile:
result = do_task(infile.read())
print(result)
print(result == 254412181)