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.
63 lines
1.4 KiB
Python
63 lines
1.4 KiB
Python
ORDERING = "J123456789TQKA"
|
|
|
|
|
|
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("123456789TQKA", [0] * len(ORDERING)))
|
|
j = 0
|
|
for card in hand:
|
|
if card != "J":
|
|
cards[card] += 1
|
|
else:
|
|
j += 1
|
|
hands_parsed.append((hand, value, cards, j))
|
|
|
|
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, j) = 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 += 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)
|