Files
SolutionEuler/solutions/0054.PokerHands/euler_54.py

220 lines
7.4 KiB
Python

"""
In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
- High Card: Highest value card.
- One Pair: Two cards of the same value.
- Two Pairs: Two different pairs.
- Three of a Kind: Three cards of the same value.
- Straight: All cards are consecutive values.
- Flush: All cards of the same suit.
- Full House: Three of a kind and a pair.
- Four of a Kind: Four cards of the same value.
- Straight Flush: All cards are consecutive values of same suit.
- Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
If two players have the same ranked hands then the rank made up of the highest value wins;
for example, a pair of eights beats a pair of fives (see example 1 below).
But if two ranks tie, for example, both players have a pair of queens,
then highest cards in each hand are compared (see example 4 below);
if the highest cards tie then the next highest cards are compared, and so on.
Consider the following five hands dealt to two players:
Hand Player 1 Player 2 Winner
1 5H 5C 6S 7S KD 2C 3S 8S 8D TD Player 2
Pair of Fives Pair of Eights
2 5D 8C 9S JS AC 2C 5C 7D 8S QH Player 1
Highest card Ace Highest card Queen
3 2D 9C AS AH AC 3D 6D 7D TD QD Player 2
Three Aces Flush with Diamonds
4 4D 6S 9H QH QC 3D 6D 7H QD QS Player 1
Pair of Queens Pair of Queens
Highest card Nine Highest card Seven
5 2H 2D 4C 4D 4S 3C 3D 3S 9S 9D Player 1
Full House Full House
With Three Fours with Three Threes
The file, poker.txt, contains one-thousand random hands dealt to two players.
Each line of the file contains ten cards (separated by a single space):
the first five are Player 1's cards and the last five are Player 2's cards.
You can assume that all hands are valid (no invalid characters or repeated cards),
each player's hand is in no specific order, and in each hand there is a clear winner.
How many hands does Player 1 win?
"""
import time
from collections import defaultdict
from pathlib import Path
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"{func.__name__} time: {elapsed_time:.6f} seconds")
return result
return wrapper
class PokerHand:
def __init__(self, hand: tuple[str, ...]):
self.hand = hand
self._rank = self.get_rank()
def __len__(self) -> int:
return len(self.hand)
def _is_same_suit(self) -> bool:
return len(set(card[1] for card in self.hand)) == 1
def _is_flush(self) -> bool:
keyflush = "23456789TJQKA"
index = []
for card in self.hand:
index.append(keyflush.index(card[0]))
if len(set(index)) != 5:
return False
return (max(index) - min(index)) == (self.__len__() - 1)
def _high_card(self) -> int:
key = "23456789TJQKA"
return max(key.index(card[0]) for card in self.hand) + 1
def _same_number(self) -> tuple[int | None, int | None, int | None]:
keyflush = "23456789TJQKA"
counts = defaultdict(int)
for card in self.hand:
if card[0] in counts:
counts[card[0]] += 1
else:
counts[card[0]] = 1
if 2 in counts.values():
pair = list(
keyflush.index(key) + 1 for key, value in counts.items() if value == 2
)
else:
pair = []
max_count = max(counts.values())
if max_count > 1:
num = max(
keyflush.index(key) + 1
for key, value in counts.items()
if value == max_count
)
if max_count == 2:
return 2, num, min(pair) if pair else None
return max_count, num, max(pair) if pair else None
return None, None, None
def get_rank(self) -> tuple[int | None, ...]:
res: list[int | None] = [None for _ in range(10)]
if self._is_same_suit():
if sorted(card[0] for card in self.hand) == list("AJKQT"):
res[0] = 1
res[1] = 1
res[4] = self._high_card()
res[5] = self._high_card()
elif self._is_flush():
res[0] = 0
res[1] = 1
res[4] = self._high_card()
res[5] = self._high_card()
else:
res[0] = 0
res[1] = 0
res[4] = self._high_card()
res[5] = 0
else:
if self._is_flush():
res[5] = self._high_card()
else:
res[5] = 0
res[0] = 0
res[1] = 0
res[4] = 0
max_count, num, pair = self._same_number()
if max_count is not None:
if max_count == 4:
res[2] = num
res[3] = 0
res[6] = 0
res[7] = 0
res[8] = 0
elif max_count == 3:
res[2] = 0
res[6] = num
res[7] = 0
if pair is not None:
res[3] = 1
res[8] = pair
else:
res[3] = 0
res[8] = 0
elif max_count == 2:
res[2] = 0
res[3] = 0
res[6] = 0
if pair is not None and pair != num:
res[7] = 1
res[8] = num
else:
res[7] = 0
res[8] = num
else:
res[2] = 0
res[3] = 0
res[6] = 0
res[7] = 0
res[8] = 0
res[9] = self._high_card()
return tuple(res)
def __gt__(self, other: "PokerHand") -> bool:
if len(self) != len(other):
raise ValueError("Cannot compare hands of different lengths")
for a, b in zip(self._rank, other._rank):
if a is None:
if b is not None:
return False
continue
else:
if b is None:
return True
if a != b:
return a > b
return False
def read_poker_hands() -> list[tuple[tuple[str, ...], tuple[str, ...]]]:
rPath = Path(__file__).parent / "0054_poker.txt"
with open(rPath, "r") as f:
lines = f.readlines()
res = []
for line in lines:
tmp = line.replace("\n", "").split(" ")
if len(tmp) < 10:
raise ValueError("Invalid line in poker.txt")
res.append((tuple(tmp[:5]), tuple(tmp[5:])))
return res
@timer
def main() -> int:
player1_win = 0
data = read_poker_hands()
for hand1, hand2 in data:
if PokerHand(hand1) > PokerHand(hand2):
player1_win += 1
return player1_win
if __name__ == "__main__":
player1_win = main()
print(f"Player 1 wins {player1_win} hands out of {len(read_poker_hands())}.")