-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler54.py
177 lines (139 loc) · 4.74 KB
/
euler54.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
import itertools
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def getRank(self):
return self.rank
def getSuit(self):
return self.suit
def __lt__(self, other):
if isinstance(other, Card):
return self.rank < other.rank
return NotImplemented
class Hand:
def __init__(self, cards):
self.cards = cards
def getCards(self):
return self.cards
def getCard(self, index):
return self.cards[index]
def setCard(self, index, card):
self.cards[index] = card
def addCard(self, card):
self.cards.append(card)
def getNthHighest(self, n):
return self.cards[4 - n].rank
def getPair(self):
try:
return next((x, y) for x, y in itertools.combinations(
self.cards, 2) if x.rank == y.rank and x != y)
except:
return None
def getTwoPairs(self):
try:
return next(
((x, y), (a, b)) for x, y, a, b in itertools.combinations(
self.cards, 4) if (
(x.rank == y.rank and x != y) and (
a.rank == b.rank and a != b)) and x != a and x != b)
except:
return None
def getThreeOfAKind(self):
try:
return next((x, y, z) for x, y, z in zip(self.cards, self.cards[
1:], self.cards[2:]) if x.rank == y.rank == z.rank)
except:
return None
def getStraight(self):
temp = [x for (x, y) in zip(self.cards, self.cards[1:])
if x.rank + 1 == y.rank]
temp.append(self.cards[4])
return self.cards if temp == self.cards else None
def getFlush(self):
return self.cards if [x for x in self.cards if x.getSuit(
) == self.cards[0].getSuit()] == self.cards else None
def getFullHouse(self):
if self.getThreeOfAKind() == None:
return None
# See if there is a pair left if you remove the three of a kind
remaining = [x for x in self.cards if x not in self.getThreeOfAKind()]
if remaining[0].rank == remaining[1].rank:
return self.cards
return None
def getFourOfAKind(self):
foakList = [(w, x, y, z) for w, x, y, z in (list(zip(self.cards, self.cards[
1:], self.cards[2:], self.cards[3:]))) if w.rank == x.rank == y.rank == z.rank]
return foakList if len(foakList) > 0 else None
def getStraightFlush(self):
if self.getStraight() and self.getFlush():
return self.cards
return None
def getRoyalFlush(self):
if self.getStraightFlush() != None and self.cards[0].rank == 10:
return self.cards
return None
def getScore(self):
if self.getRoyalFlush():
return (23, 23)
elif self.getStraightFlush():
return (22, self.getStraightFlush()[0].rank)
elif self.getFourOfAKind():
return (21, self.getFourOfAKind()[0].rank)
elif self.getFullHouse():
return (20, self.getThreeOfAKind()[0].rank)
elif self.getFlush():
return (19, self.getFlush()[0].rank)
elif self.getStraight():
return (18, self.getStraight()[0].rank)
if self.getThreeOfAKind():
return (17, self.getThreeOfAKind()[0].rank)
elif self.getTwoPairs():
return (16, 0)
elif self.getPair():
return (15, self.getPair()[0].rank)
else:
return self.getNthHighest(0)
def __gt__(self, other):
if isinstance(other, Hand):
thisScore = self.getScore()
otherScore = other.getScore()
if thisScore == otherScore:
i = 0
while(self.getNthHighest(i) == other.getNthHighest(i)):
i += 1
return self.getNthHighest(i) > other.getNthHighest(i)
else:
return thisScore > otherScore
return NotImplemented
def getCardScore(x):
if x == 'T':
return 10
elif x == 'J':
return 11
elif x == 'Q':
return 12
elif x == 'K':
return 13
elif x == 'A':
return 14
else:
return int(x)
playerOneWins = 0
lines = [line.strip() for line in open('poker.txt')]
for hands in lines:
tempList = hands.split()[:5]
list1 = []
for x in tempList:
card = Card(getCardScore(x[0]), x[1])
list1.append(card)
h1 = Hand(sorted(list1))
tempList1 = hands.split()[5:]
list2 = []
for x in tempList1:
card = Card(getCardScore(x[0]), x[1])
list2.append(card)
h2 = Hand(sorted(list2))
if h1 > h2:
playerOneWins += 1
print(playerOneWins)