-
Notifications
You must be signed in to change notification settings - Fork 1
/
54.py
244 lines (197 loc) · 6.87 KB
/
54.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import collections
import functools
@functools.total_ordering
class Evaluation:
# Types of poker hands, least-valuable first.
high_card = "0_high_card"
one_pair = "1_one_pair"
two_pairs = "2_two_pairs"
three_of_a_kind = "3_three_of_a_kind"
straight = "4_straight"
flush = "5_flush"
full_house = "6_full_house"
four_of_a_kind = "7_four_of_a_kind"
straight_flush = "8_straight_flush"
royal_flush = "9_royal_flush"
# Shorthands for non-numeric cards
t = 10
j = 11
q = 12
k = 13
a = 14
def __init__(self):
self.flavor = None
self.ordered_for_comparison = ()
def __str__(self):
return "{}: {}".format(self.flavor, self.ordered_for_comparison)
def __eq__(self, other):
return self.total_value() == other.total_value()
def total_value(self):
return (self.flavor, self.ordered_for_comparison)
def __lt__(self, other):
return self.total_value() < other.total_value()
def rank(card):
first_letter = card[0]
if first_letter.isdigit():
return int(first_letter)
return {"t": 10, "j": 11, "q": 12, "k": 13, "a": 14}[first_letter.lower()]
def suit(card):
return card[1].lower()
def is_flush(cards):
suits = [suit(c) for c in cards]
return len(set(suits)) == 1
def is_straight(cards):
ranks = set([rank(c) for c in cards])
if len(ranks) != len(cards):
return False
return max(ranks) - min(ranks) == len(ranks) - 1
def evaluate_hand(hand):
e = Evaluation()
cards = hand.split()
ranks_descending = sorted([rank(c) for c in cards], reverse=True)
suits = [suit(c) for c in cards]
by_ranks = collections.defaultdict(set)
for c in cards:
by_ranks[rank(c)].add(suit(c))
rank_histogram = {k: len(v) for k, v in by_ranks.items()}
shape = sorted(rank_histogram.values(), reverse=True)
ranks_by_number_of_occurrences = {v: k for k, v in rank_histogram.items()}
if is_straight(cards) and is_flush(cards):
if ranks_descending[0] == Evaluation.a:
e.flavor = e.royal_flush
else:
e.flavor = e.straight_flush
e.ordered_for_comparison = tuple(ranks_descending)
elif shape == [4, 1]:
e.flavor = e.four_of_a_kind
e.ordered_for_comparison = tuple(
[ranks_by_number_of_occurrences[4]] * 4
+ [ranks_by_number_of_occurrences[1]]
)
elif shape == [3, 2]:
e.flavor = e.full_house
e.ordered_for_comparison = tuple(
[ranks_by_number_of_occurrences[3]] * 3
+ [ranks_by_number_of_occurrences[2]] * 2
)
elif len(set(suits)) == 1:
e.flavor = e.flush
e.ordered_for_comparison = tuple(ranks_descending)
elif is_straight(cards):
e.flavor = e.straight
e.ordered_for_comparison = tuple(ranks_descending)
elif 3 in ranks_by_number_of_occurrences:
r = ranks_by_number_of_occurrences.pop(3)
rank_histogram.pop(r)
e.flavor = e.three_of_a_kind
e.ordered_for_comparison = tuple(
[r] * 3 + sorted(rank_histogram.keys(), reverse=True)
)
elif shape == [2, 2, 1]:
e.flavor = e.two_pairs
ranks_of_pairs = [r for r, cards in by_ranks.items() if len(cards) == 2]
e.ordered_for_comparison = sorted(
[
ranks_of_pairs[0],
ranks_of_pairs[0],
ranks_of_pairs[1],
ranks_of_pairs[1],
],
reverse=True,
)
e.ordered_for_comparison.append(ranks_by_number_of_occurrences[1])
e.ordered_for_comparison = tuple(e.ordered_for_comparison)
elif shape == [2, 1, 1, 1]:
r = ranks_by_number_of_occurrences.pop(2)
rank_histogram.pop(r)
e.flavor = e.one_pair
e.ordered_for_comparison = tuple(
[r] * 2 + sorted(rank_histogram.keys(), reverse=True)
)
else:
e.flavor = e.high_card
e.ordered_for_comparison = tuple(ranks_descending)
return e
def test_is_straight():
assert is_straight("TS 9C 8H 7D JS".split())
assert not is_straight("5z 5z 9z 9z 7z".split())
def test_evaluate_high_card():
hand = "8C TS KC 9H 4S"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.high_card
assert value.ordered_for_comparison == (Evaluation.k, Evaluation.t, 9, 8, 4)
def test_evaluate_one_pair():
hand = "8C TS KC 9H 8S"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.one_pair
assert value.ordered_for_comparison == (8, 8, Evaluation.k, Evaluation.t, 9)
def test_evaluate_two_pair():
hand = "8C TS TC 8H 4S"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.two_pairs
assert value.ordered_for_comparison == (Evaluation.t, Evaluation.t, 8, 8, 4)
def test_evaluate_three_of_a_kind():
hand = "8C TS 9C 8H 8S"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.three_of_a_kind
assert value.ordered_for_comparison == (8, 8, 8, Evaluation.t, 9)
def test_evaluate_straight():
hand = "8C TS 9C QH JS"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.straight
assert value.ordered_for_comparison == (
Evaluation.q,
Evaluation.j,
Evaluation.t,
9,
8,
)
def test_evaluate_flush():
hand = "8C TC 2C QC JC"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.flush
assert value.ordered_for_comparison == (
Evaluation.q,
Evaluation.j,
Evaluation.t,
8,
2,
)
def test_evaluate_full_house():
hand = "8C 8S 2D 2C 2H"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.full_house
assert value.ordered_for_comparison == (2, 2, 2, 8, 8)
def test_evaluate_four_of_a_kind():
hand = "8C 8S 8D 8H 2H"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.four_of_a_kind
assert value.ordered_for_comparison == (8, 8, 8, 8, 2)
def test_evaluate_straight_flush():
hand = "8H 7H 6H 5H 4H"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.straight_flush
assert value.ordered_for_comparison == (8, 7, 6, 5, 4)
def test_evaluate_royal_flush():
hand = "AH QH TH KH JH"
value = evaluate_hand(hand)
assert value.flavor == Evaluation.royal_flush
assert value.ordered_for_comparison == (
Evaluation.a,
Evaluation.k,
Evaluation.q,
Evaluation.j,
Evaluation.t,
)
if __name__ == "__main__":
with open("p054_poker.txt") as inf:
wins = 0
for line in inf:
if not (line[0].isdigit()) and not (line[0]) in {"T", "J", "Q", "K", "A"}:
print("Skipping {}".format(line))
continue
player_1 = line[0:14]
player_2 = line[14:]
if evaluate_hand(player_1) > evaluate_hand(player_2):
wins += 1
print(wins)