day 7 part 1
This commit is contained in:
parent
4fc7d32522
commit
1becb4b4bf
30
day7/day7.py
30
day7/day7.py
@ -3,27 +3,24 @@ from collections import Counter
|
|||||||
def calculate_rank(hand):
|
def calculate_rank(hand):
|
||||||
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
|
# substitute cards with their ranks
|
||||||
hand = sorted(card_ranks.get(c, None) or c for c in hand)[::-1]
|
hand = [card_ranks[c] for c in hand]
|
||||||
cnt = Counter(hand)
|
cnt = Counter(hand)
|
||||||
cnt_values = sorted(cnt.values())
|
rank = 0
|
||||||
rank = {
|
match sorted(cnt.values()):
|
||||||
"5": 10,
|
case [5]: rank = 7
|
||||||
"14": 9,
|
case [1, 4]: rank = 6
|
||||||
"23": 8,
|
case [2, 3]: rank = 5
|
||||||
"113": 7,
|
case [1, 1, 3]: rank = 4
|
||||||
"122": 6,
|
case [1, 2, 2]: rank = 3
|
||||||
"1112": 5,
|
case [1, 1, 1, 2]: rank = 2
|
||||||
"11111": 4,
|
case [1, 1, 1, 1, 1]: rank = 1
|
||||||
}["".join(map(str, cnt_values))]
|
|
||||||
# return rank and sorted hand as a tiebreaker
|
|
||||||
return (rank, hand)
|
return (rank, hand)
|
||||||
|
|
||||||
|
|
||||||
def part1(inp):
|
def part1(inp):
|
||||||
total = 0
|
total = 0
|
||||||
hands = [l.strip().split() for l in inp.strip().split("\n")]
|
hands = [l.strip().split() for l in inp.strip().split("\n")]
|
||||||
print(hands[-10: -1])
|
|
||||||
breakpoint()
|
|
||||||
hands = sorted(hands, key=lambda hb: calculate_rank(hb[0]))
|
hands = sorted(hands, key=lambda hb: calculate_rank(hb[0]))
|
||||||
for rank, hand in enumerate(hands):
|
for rank, hand in enumerate(hands):
|
||||||
hand, bid = hand
|
hand, bid = hand
|
||||||
@ -39,11 +36,12 @@ if __name__ == "__main__":
|
|||||||
KTJJT 220
|
KTJJT 220
|
||||||
QQQJA 483
|
QQQJA 483
|
||||||
"""
|
"""
|
||||||
assert part1(sample_input) == 6440
|
res = part1(sample_input)
|
||||||
|
print(f"part 1 example: {res}")
|
||||||
|
assert res == 6440
|
||||||
|
|
||||||
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())
|
res = part1(f.read())
|
||||||
print(f"Part 1, res={res}")
|
print(f"Part 1, res={res}")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user