-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem54.py
More file actions
78 lines (67 loc) · 1.97 KB
/
Problem54.py
File metadata and controls
78 lines (67 loc) · 1.97 KB
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
from collections import Counter
from operator import itemgetter
def Solve():
faceCards = {'T': 10, 'J':11, 'Q':12, 'K':13, 'A':14}
def HandScore(hand):
values = Counter()
suits = set()
for card in hand:
rank = card[0]
try:
v = int(rank)
except:
v = faceCards[rank]
values[v] += 1
suits.add(card[1])
isStraight = max(values.keys()) == min(values.keys()) + 4 and len(values) == 5
isFlush = suits.pop() == 5
if isStraight and isFlush:
score = 8
elif max(values.values()) == 4:
score = 7
elif max(values.values()) == 3 and max(values.values()) == 2:
score = 6
elif isFlush:
score = 5
elif isStraight:
score = 4
elif max(values.values()) == 3:
score = 3
elif values.most_common(2)[0][1] == 2 and values.most_common(2)[1][1] == 2:
score = 2
elif values.most_common(2)[0][1] == 2:
score = 1
else:
score = 0
tieBreakSet = values.most_common()
tieBreakSet = [(y,x) for (x,y) in tieBreakSet]
tieBreakSet.sort(reverse = True)
tieBreakSet = sum([[x]*y for y, x in tieBreakSet],[])
tieBreak = 0
for v in tieBreakSet:
tieBreak *= 15
tieBreak += v
score *= 15**5
score += tieBreak
return score
file = open('./data/p054_poker.txt')
hands = file.read()
file.close()
hands = hands.split('\n')
count = 0
blah = []
i = 0
for hand in hands:
try:
hand = hand.split(' ')
hand1 = hand[0:5]
hand2 = hand[5:10]
score1 = HandScore(hand1)
score2 = HandScore(hand2)
if score1 > score2:
count += 1
except:
pass
return count
if __name__ == '__main__':
print(Solve())