-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.py
62 lines (50 loc) · 1.21 KB
/
day7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Day 7: Camel Cards
"""
from collections import Counter
SAMPLE_INPUT = """
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483
"""
# pylint: disable=too-many-return-statements
def _rank(hand):
counts = Counter(x for x in hand if x >= 0).most_common(2)
count0 = counts[0][1] if counts else 0
count1 = counts[1][1] if len(counts) > 1 else 0
jokers = sum(x < 0 for x in hand)
if count0 + jokers >= 5:
return 6
if count0 + jokers >= 4:
return 5
if count0 + count1 + jokers >= 5:
return 4
if count0 + jokers >= 3:
return 3
if count0 + count1 + jokers >= 4:
return 2
if count0 + jokers >= 2:
return 1
return 0
def _solve(cards, data):
hands = [
(_rank(hand := tuple(map(cards.find, words[0]))), hand, int(words[1]))
for line in data.splitlines()
if len(words := line.split(maxsplit=1)) == 2
]
return sum((i + 1) * bid for i, (_, _, bid) in enumerate(sorted(hands)))
def part1(data):
"""
>>> part1(SAMPLE_INPUT)
6440
"""
return _solve("23456789TJQKA", data)
def part2(data):
"""
>>> part2(SAMPLE_INPUT)
5905
"""
return _solve("23456789TQKA", data)
parts = (part1, part2)