day 7 part 2
This commit is contained in:
parent
1becb4b4bf
commit
d74a0efc80
41
day7/day7.py
41
day7/day7.py
@ -1,10 +1,21 @@
|
|||||||
from collections import Counter
|
from collections import Counter
|
||||||
|
from re import sub
|
||||||
|
|
||||||
def calculate_rank(hand):
|
def calculate_rank(hand, part2=False):
|
||||||
card_ranks = {'2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12, 'A': 13}
|
card_ranks = {'2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7, '9': 8, 'T': 9, 'J': 10, 'Q': 11, 'K': 12, 'A': 13}
|
||||||
# substitute cards with their ranks
|
if part2:
|
||||||
|
# in part2, jokers are the weakest card
|
||||||
|
card_ranks['J'] = 0
|
||||||
|
# substitute cards with their ranks to make them sortable
|
||||||
hand = [card_ranks[c] for c in hand]
|
hand = [card_ranks[c] for c in hand]
|
||||||
cnt = Counter(hand)
|
cnt = Counter(hand)
|
||||||
|
|
||||||
|
if part2 and cnt[0] != 5: # edge case if hand == 'JJJJJ'
|
||||||
|
# substitute jokers with the most common card that isn't a joker
|
||||||
|
most_common_card = cnt.most_common(2)[0][0] if cnt.most_common(2)[0][0] != 0 else cnt.most_common(2)[1][0]
|
||||||
|
new_hand = [most_common_card if c == 0 else c for c in hand]
|
||||||
|
cnt = Counter(new_hand)
|
||||||
|
|
||||||
rank = 0
|
rank = 0
|
||||||
match sorted(cnt.values()):
|
match sorted(cnt.values()):
|
||||||
case [5]: rank = 7
|
case [5]: rank = 7
|
||||||
@ -14,14 +25,17 @@ def calculate_rank(hand):
|
|||||||
case [1, 2, 2]: rank = 3
|
case [1, 2, 2]: rank = 3
|
||||||
case [1, 1, 1, 2]: rank = 2
|
case [1, 1, 1, 2]: rank = 2
|
||||||
case [1, 1, 1, 1, 1]: rank = 1
|
case [1, 1, 1, 1, 1]: rank = 1
|
||||||
|
# return rank, and hand as a tiebreaker
|
||||||
return (rank, hand)
|
return (rank, hand)
|
||||||
|
|
||||||
|
def parse_input(inp):
|
||||||
def part1(inp):
|
|
||||||
total = 0
|
|
||||||
hands = [l.strip().split() for l in inp.strip().split("\n")]
|
hands = [l.strip().split() for l in inp.strip().split("\n")]
|
||||||
hands = sorted(hands, key=lambda hb: calculate_rank(hb[0]))
|
return hands
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_wins(hands, part2=False):
|
||||||
|
total = 0
|
||||||
|
hands = sorted(hands, key=lambda hb: calculate_rank(hb[0], part2=part2))
|
||||||
for rank, hand in enumerate(hands):
|
for rank, hand in enumerate(hands):
|
||||||
hand, bid = hand
|
hand, bid = hand
|
||||||
total += (rank + 1) * int(bid)
|
total += (rank + 1) * int(bid)
|
||||||
@ -36,12 +50,21 @@ if __name__ == "__main__":
|
|||||||
KTJJT 220
|
KTJJT 220
|
||||||
QQQJA 483
|
QQQJA 483
|
||||||
"""
|
"""
|
||||||
res = part1(sample_input)
|
res = calculate_wins(parse_input(sample_input))
|
||||||
print(f"part 1 example: {res}")
|
print(f"part 1 example: {res}")
|
||||||
assert res == 6440
|
assert res == 6440
|
||||||
|
|
||||||
|
res = calculate_wins(parse_input(sample_input), part2=True)
|
||||||
|
print(f"part 2 example: {res}")
|
||||||
|
assert res == 5905
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
if len(sys.argv) == 2:
|
if len(sys.argv) == 2:
|
||||||
with open(sys.argv[1]) as f:
|
with open(sys.argv[1]) as f:
|
||||||
res = part1(f.read())
|
inp = parse_input(f.read())
|
||||||
|
|
||||||
|
res = calculate_wins(inp)
|
||||||
print(f"Part 1, res={res}")
|
print(f"Part 1, res={res}")
|
||||||
|
|
||||||
|
res = calculate_wins(inp, part2=True)
|
||||||
|
print(f"Part 2, res={res}")
|
||||||
|
Loading…
Reference in New Issue
Block a user