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

11 months ago
ORDERING = "J123456789TQKA"
def do_task(infile):
hands = [(line.split()[0], int(line.split()[1])) for line in infile.split("\n")]
11 months ago
hands.sort(key=evaluate_hand)
11 months ago
score_sum = 0
11 months ago
for rank, (_, score) in enumerate(hands, start=1):
11 months ago
score_sum += rank * score
return score_sum
def evaluate_hand(h):
11 months ago
(hand, _) = h
cards = dict.fromkeys("123456789TQKA", 0)
j = 0
for card in hand:
if card != "J":
cards[card] += 1
else:
j += 1
11 months ago
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)