D4y Se7en

main
HeNine 11 months ago
parent b14b77adc0
commit 00d4f8cfd1

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

File diff suppressed because it is too large Load Diff

@ -0,0 +1,56 @@
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)

@ -0,0 +1,62 @@
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)
Loading…
Cancel
Save