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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
values = {
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'T': 10,
'J': 11,
'Q': 12,
'K': 13,
'A': 14
}
colors = {
'H': 1,
'C': 2,
'S': 3,
'D': 4
}
def handvalue(hand):
return values[hand[0]]*10 + colors[hand[1]]
def count_cards(hand):
hand = [ x/10 for x in hand ]
counter = { }
for c in hand:
if c in counter:
counter[c] += 1
else:
counter[c] = 1
return counter
def high_card(hand):
hand = [ x/10 for x in hand ]
hand.sort()
return hand[-1]
def is_flush(hand):
return (hand[0]%10 == hand[1]%10 == hand[2]%10 == hand[3]%10 == hand[4]%10)
def is_straight(hand):
hand = [ x/10 for x in hand ]
hand.sort()
return (hand[4] == hand[3]+1 == hand[2]+2 == hand[1]+3 == hand[0]+4)
def is_straight_flush(hand):
return is_flush(hand) and is_straight(hand)
def is_royal_flush(hand):
return is_straight_flush(hand) and high_card(hand) == values['A']
def is_n_of_a_kind(hand, n):
counter = count_cards(hand)
for c in counter:
if counter[c] == n:
return True
return False
def is_four_of_a_kind(hand):
return is_n_of_a_kind(hand, 4)
def is_three_of_a_kind(hand):
return is_n_of_a_kind(hand, 3)
def is_one_pair(hand):
return is_n_of_a_kind(hand, 2)
def is_two_pairs(hand):
counter = count_cards(hand)
pairs = 0
for c in counter:
if counter[c] == 2 or counter[c] == 3:
pairs += 1
return pairs == 2
def is_full_house(hand):
return is_two_pairs(hand) and is_three_of_a_kind(hand)
def better_hand(hand1, hand2):
# both are straight flush
if is_straight_flush(hand1):
if high_card(hand1) > high_card(hand2):
return 1
else:
return 2
# both are four of a kind
if is_four_of_a_kind(hand1):
count1 = count_cards(hand1)
count2 = count_cards(hand2)
value4_1 = 0
value4_2 = 0
for c in count1:
if count1[c] == 4:
value4_1 = c
break
for c in count2:
if count2[c] == 4:
value4_2 = c
break
if value4_1 > value4_2:
return 1
else:
return 2
# both are full house
if is_full_house(hand1):
count1 = count_cards(hand1)
count2 = count_cards(hand2)
value3_1 = 0
value3_2 = 0
for c in count1:
if count1[c] == 3:
value3_1 = c
break
for c in count2:
if count2[c] == 3:
value3_2 = c
break
if value3_1 > value3_2:
return 1
else:
return 2
# both are flush
if is_flush(hand1):
hand1 = [ x/10 for x in hand1 ]
hand2 = [ x/10 for x in hand2 ]
hand1.sort()
hand2.sort()
for i in range(0, 5):
if hand1[4-i] > hand2[4-i]:
return 1
elif hand2[4-i] > hand1[4-i]:
return 2
print "ERROR: no winner with hands: ", hand1, hand2 # should never be reached
# both are straight
if is_straight(hand1):
if high_card(hand1) > high_card(hand2):
return 1
else:
return 2
# both are three of a kind
if is_three_of_a_kind(hand1):
count1 = count_cards(hand1)
count2 = count_cards(hand2)
value3_1 = 0
value3_2 = 0
for c in count1:
if count1[c] == 3:
value3_1 = c
break
for c in count2:
if count2[c] == 3:
value3_2 = c
break
if value3_1 > value3_2:
return 1
else:
return 2
# both are two pairs
if is_two_pairs(hand1):
count1 = count_cards(hand1)
count2 = count_cards(hand2)
value2_1 = []
value1_1 = 0
value2_2 = []
value1_2 = 0
for c in count1:
if count1[c] == 2:
value2_1.append(c)
elif count1[c] == 1:
value1_1 = c
for c in count2:
if count2[c] == 2:
value2_2.append(c)
elif count1[c] == 1:
value1_2 = c
value2_1.sort()
value2_2.sort()
if value2_1[1] > value2_2[1]:
return 1
elif value2_2[1] > value2_1[1]:
return 2
elif value2_1[0] > value2_2[0]:
return 1
elif value2_2[0] > value2_1[0]:
return 2
elif value1_1 > value1_2:
return 1
else:
return 2
# both are one pair
if is_one_pair(hand1):
count1 = count_cards(hand1)
count2 = count_cards(hand2)
value2_1 = 0
value1_1 = []
value2_2 = 0
value1_2 = []
for c in count1:
if count1[c] == 2:
value2_1 = c
elif count1[c] == 1:
value1_1.append(c)
for c in count2:
if count2[c] == 2:
value2_2 = c
elif count2[c] == 1:
value1_2.append(c)
value1_1.sort()
value1_2.sort()
if value2_1 > value2_2:
return 1
elif value2_2 > value2_1:
return 2
elif high_card(value1_1) > high_card(value1_2):
return 1
else:
return 2
# both are high card
if high_card(hand1) > high_card(hand2):
return 1
else:
return 2
def rank(hand):
if is_royal_flush(hand):
return 10
if is_straight_flush(hand):
return 9
if is_four_of_a_kind(hand):
return 8
if is_full_house(hand):
return 7
if is_flush(hand):
return 6
if is_straight(hand):
return 5
if is_three_of_a_kind(hand):
return 4
if is_two_pairs(hand):
return 3
if is_one_pair(hand):
return 2
return 1 # high card
def winner(hand1, hand2):
if rank(hand1) > rank(hand2):
return 1
if rank(hand2) > rank(hand1):
return 2
return better_hand(hand1, hand2)
hands = []
f = open('054.txt', 'r')
for line in f:
line.rstrip('\n')
hands.append([ handvalue(x) for x in line.split(' ') ])
f.close()
count_win1 = 0
for h in hands:
hand1 = h[:5]
hand2 = h[5:]
if winner(hand1, hand2) == 1:
count_win1 += 1
print count_win1
|