Python Implementation of Problem 54
View source code here on GitHub!
Includes
Problem Solution
Project Euler Problem 54
Problem:
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 Pair of Fives |
2C 3S 8S 8D TD Pair of Eights |
Player 2 |
2 |
5D 8C 9S JS AC Highest card Ace |
2C 5C 7D 8S QH Highest card Queen |
Player 1 |
3 |
2D 9C AS AH AC Three Aces |
3D 6D 7D TD QD Flush with Diamonds |
Player 2 |
4 |
4D 6S 9H QH QC Pair of Queens Highest card Nine |
3D 6D 7H QD QS Pair of Queens Highest card Seven |
Player 1 |
5 |
2H 2D 4C 4D 4S Full House With Three Fours |
3C 3D 3S 9S 9D Full House with Three Threes |
Player 1 |
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?
1"""
2Project Euler Problem 54
3
4Problem:
5
6In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
7
8- **High Card**: Highest value card.
9- **One Pair**: Two cards of the same value.
10- **Two Pairs**: Two different pairs.
11- **Three of a Kind**: Three cards of the same value.
12- **Straight**: All cards are consecutive values.
13- **Flush**: All cards of the same suit.
14- **Full House**: Three of a kind and a pair.
15- **Four of a Kind**: Four cards of the same value.
16- **Straight Flush**: All cards are consecutive values of same suit.
17- **Royal Flush**: Ten, Jack, Queen, King, Ace, in same suit.
18
19The cards are valued in the order: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
20
21If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of
22eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of
23queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next
24highest cards are compared, and so on.
25
26Consider the following five hands dealt to two players:
27
28====== ================================================= ================================================== ==========
29 Hand Player 1 Player 2 Winner
30====== ================================================= ================================================== ==========
31 1 5H 5C 6S 7S KD Pair of Fives 2C 3S 8S 8D TD Pair of Eights Player 2
32 2 5D 8C 9S JS AC Highest card Ace 2C 5C 7D 8S QH Highest card Queen Player 1
33 3 2D 9C AS AH AC Three Aces 3D 6D 7D TD QD Flush with Diamonds Player 2
34 4 4D 6S 9H QH QC Pair of Queens Highest card Nine 3D 6D 7H QD QS Pair of Queens Highest card Seven Player 1
35 5 2H 2D 4C 4D 4S Full House With Three Fours 3C 3D 3S 9S 9D Full House with Three Threes Player 1
36====== ================================================= ================================================== ==========
37
38The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards
39(separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can
40assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order,
41and in each hand there is a clear winner.
42
43How many hands does Player 1 win?
44"""
45from collections import Counter
46from statistics import multimode
47from typing import Sequence, Tuple
48
49from .lib.utils import get_data_file
50
51numerals = {
52 'T': 10,
53 'J': 11,
54 'Q': 12,
55 'K': 13,
56 'A': 14,
57}
58numerals.update({
59 str(x): x for x in range(2, 10)
60})
61
62HandType = Sequence[Tuple[int, str]]
63
64
65def main() -> int:
66 p1_wins = 0
67 for line in get_data_file("p0054_poker.txt").splitlines():
68 cards = [parse_card(x) for x in line.split()]
69 p1 = sorted(cards[:5])
70 p2 = sorted(cards[5:])
71 v1 = assign_value(p1)
72 v2 = assign_value(p2)
73 print(p1, v1, p2, v2)
74 if v1 > v2:
75 p1_wins += 1
76 return p1_wins
77
78
79def parse_card(card: str) -> Tuple[int, str]:
80 return numerals[card[0]], card[1]
81
82
83def assign_value(hand: HandType) -> Tuple[float, int]:
84 highest_card = max(card[0] for card in hand)
85 if has_royal_flush(hand):
86 return (9, highest_card)
87 if has_straight_flush(hand):
88 return (8, highest_card)
89 modal_card = max(multimode(card[0] for card in hand))
90 for idx, test in enumerate((
91 has_four_of_a_kind,
92 has_full_house,
93 has_flush,
94 has_straight,
95 has_three_of_a_kind,
96 has_two_pairs,
97 has_two_of_a_kind
98 )):
99 if test(hand):
100 return (7 - idx, modal_card)
101 return (0, highest_card)
102
103
104def has_royal_flush(hand: HandType) -> bool:
105 suits = set(card[1] for card in hand)
106 if len(suits) == 1:
107 values = [card[0] for card in hand]
108 return values == list(range(10, 15))
109 return False
110
111
112def has_straight_flush(hand: HandType) -> bool:
113 return has_straight(hand) and has_flush(hand)
114
115
116def has_flush(hand: HandType) -> bool:
117 return len(set(card[1] for card in hand)) == 1
118
119
120def has_straight(hand: HandType) -> bool:
121 values = [card[0] for card in hand]
122 return values == list(range(min(values), max(values) + 1))
123
124
125def has_four_of_a_kind(hand: HandType) -> bool:
126 return 4 in Counter(card[0] for card in hand).values()
127
128
129def has_full_house(hand: HandType) -> bool:
130 return has_three_of_a_kind(hand) and has_two_of_a_kind(hand)
131
132
133def has_three_of_a_kind(hand: HandType) -> bool:
134 return 3 in Counter(card[0] for card in hand).values()
135
136
137def has_two_pairs(hand: HandType) -> bool:
138 return [1, 2, 2] == sorted(Counter(card[0] for card in hand).values())
139
140
141def has_two_of_a_kind(hand: HandType) -> bool:
142 return 2 in Counter(card[0] for card in hand).values()