Skip to content

Commit c204636

Browse files
committed
day7
1 parent 5c3fef2 commit c204636

File tree

3 files changed

+1112
-1
lines changed

3 files changed

+1112
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
env
2-
32
.cache
3+
__pycache__

2023/day7.py

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from common import ai
2+
from math import sqrt, ceil, floor
3+
from collections import Counter
4+
5+
inp = """
6+
32T3K 765
7+
T55J5 684
8+
KK677 28
9+
KTJJT 220
10+
QQQJA 483
11+
"""
12+
13+
def parse(inp):
14+
res = []
15+
for l in inp.strip().split("\n"):
16+
cards,bid = l.split()
17+
res.append((cards, int(bid)))
18+
return res
19+
20+
strength = "AKQJT98765432"
21+
sd = dict([(k,i) for i,k in enumerate(reversed(strength))])
22+
23+
def score_p1(cards: str):
24+
# return tuple of (hand_type, s0, s1, s2, s3, s4, s5)
25+
ss = [sd[c] for c in cards]
26+
# hand_type is 7 thru 1
27+
c = Counter(cards)
28+
xs = tuple(sorted(c.values()))
29+
# print(xs)
30+
scores = {
31+
(5,): 7, # five of a kind
32+
(1, 4): 6, # four of a kind
33+
(2, 3): 5, # full house (3 and a pair)
34+
(1, 1, 3): 4, # three of a kind
35+
(1, 2, 2): 3, # two pair
36+
(1, 1, 1, 2): 2, # one pair
37+
(1, 1, 1, 1, 1): 1, # high card
38+
}
39+
return scores[xs], ss
40+
41+
ai(score_p1('AA8AA'), (6, [12, 12, 6, 12, 12]))
42+
ai(score_p1('23432'), (3, [0, 1, 2, 1, 0]))
43+
44+
def p1(inp, scorefn):
45+
scored = [(scorefn(cards), cards, bid) for cards,bid in parse(inp)]
46+
t = 0
47+
for i, hand in enumerate(sorted(scored)):
48+
(score, cards, bid) = hand
49+
rank = i + 1
50+
print(f" {rank:8} {cards} {bid:5} {score}")
51+
t += rank*bid
52+
return t
53+
54+
ai(p1(inp, score_p1), 6440)
55+
ai(p1(open("day7.txt").read(), score_p1), 254024898)
56+
57+
strength_p2 = "AKQT98765432J"
58+
sd_p2 = dict([(k,i) for i,k in enumerate(reversed(strength_p2))])
59+
60+
def score_p2(cards: str):
61+
# return tuple of (hand_type, s0, s1, s2, s3, s4, s5)
62+
ss = [sd_p2[c] for c in cards]
63+
# hand_type is 7 thru 1
64+
c = Counter(cards)
65+
# remove the count of Jokers
66+
js = c['J']
67+
del c['J']
68+
print(f'{cards} => {js}')
69+
xs = tuple(sorted(c.values()))
70+
# print(xs)
71+
scores = {
72+
0: {
73+
(5,): 7, # five of a kind
74+
(1, 4): 6, # four of a kind
75+
(2, 3): 5, # full house (3 and a pair)
76+
(1, 1, 3): 4, # three of a kind
77+
(1, 2, 2): 3, # two pair
78+
(1, 1, 1, 2): 2, # one pair
79+
(1, 1, 1, 1, 1): 1, # high card
80+
},
81+
1: {
82+
(4,): 7, # five of a kind
83+
(1, 3): 6, # four of a kind
84+
(2, 2): 5, # full house (3 and a pair)
85+
(1, 1, 2): 4, # three of a kind
86+
(1, 1, 1, 1): 2, # one pair
87+
},
88+
2: {
89+
(3,): 7, # five of a kind
90+
(1, 2): 6, # four of a kind
91+
(1, 1, 1): 4, # three of a kind
92+
},
93+
3: {
94+
(2,): 7, # five of a kind
95+
(1, 1): 6, # four of a kind
96+
},
97+
4: {
98+
(1,): 7, # five of a kind
99+
},
100+
5: {
101+
(): 7,
102+
},
103+
}
104+
return scores[js][xs], ss
105+
106+
ai(score_p2('AA8AA'), (6, [12, 12, 7, 12, 12]))
107+
ai(score_p2('23432'), (3, [1, 2, 3, 2, 1]))
108+
ai(score_p2('JJJJJ'), (7, [0, 0, 0, 0, 0]))
109+
110+
ai(p1(inp, score_p2), 5905)
111+
ai(p1(open("day7.txt").read(), score_p2), 254115617)

0 commit comments

Comments
 (0)