Back to Project Euler

Previous Problem Next Problem

Problem 54: Poker Hands

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

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?

Solution

I accomplish this by assigning each hand a six-digit base 13 integer representing its value, then seeing which of the hand values in each matchup is greater. Let the value of each card be the number of other card ranks it beats; so a 2 is worth 0, a 3 is worth 1, etc., a King is worth 11, and an Ace is worth 12. Hand values are assigned as follows:

In real poker, a straight can also be formed from a 5, 4, 3, 2, and Ace, with the 5 counting as the highest card; but this problem as written excludes ace-low straights, so my code does too.

Python Code

import urllib.request
def hand_value(hand_str):
	vals = sorted("23456789TJQKA".index(i) for i in hand_str[::3])
	unique_ranks = len(set(vals))
	if unique_ranks == 5:  # straight, flush, straight flush, or high card
		is_straight = vals[0] + 4 == vals[4]
		is_flush = len(set(hand_str[1::3])) == 1
		if is_straight and is_flush:
			return 8 * 13**5 + vals[4]
		elif is_straight:
			return 4 * 13**5 + vals[4]
		elif is_flush:
			return 5 * 13**5 + sum(vals[i] * 13**i for i in range(5))
		else:
			return sum(vals[i] * 13**i for i in range(5))
	elif unique_ranks == 4:  # one pair
		if vals[0] == vals[1]:
			p, c0, c1, c2 = vals[0], vals[2], vals[3], vals[4]
		elif vals[1] == vals[2]:
			p, c0, c1, c2 = vals[1], vals[0], vals[3], vals[4]
		elif vals[2] == vals[3]:
			p, c0, c1, c2 = vals[2], vals[0], vals[1], vals[4]
		else:
			p, c0, c1, c2 = vals[3], vals[0], vals[1], vals[2]
		return 1 * 13**5 + p * 13**3 + c2 * 13**2 + c1 * 13 + c0
	elif unique_ranks == 3:  # two pairs or three of a kind
		if vals[0] == vals[2] or vals[1] == vals[3] or vals[2] == vals[4]:
			if vals[0] == vals[2]:
				t, c0, c1 = vals[0], vals[3], vals[4]
			elif vals[1] == vals[3]:
				t, c0, c1 = vals[1], vals[0], vals[4]
			else:
				t, c0, c1 = vals[2], vals[0], vals[1]
			return 3 * 13**5 + t * 13**2 + c1 * 13 + c0
		else:
			if vals[0] != vals[1]:
				p0, p1, c = vals[1], vals[3], vals[0]
			elif vals[2] != vals[3]:
				p0, p1, c = vals[0], vals[3], vals[2]
			else:
				p0, p1, c = vals[0], vals[2], vals[4]
			return 2 * 13**5 + p1 * 13**2 + p0 * 13 + c
	else:  # four of a kind or full house
		if vals[0] == vals[3] or vals[1] == vals[4]:
			if vals[0] == vals[3]:
				q, c = vals[0], vals[4]
			else:
				q, c = vals[1], vals[0]
			return 7 * 13**5 + q * 13 + c
		else:
			if vals[0] == vals[2]:
				t, p = vals[0], vals[3]
			else:
				t, p = vals[2], vals[0]
			return 6 * 13**5 + t * 13 + p

def p54():
	url = "https://projecteuler.net/resources/documents/0054_poker.txt"
	f = urllib.request.urlopen(url)
	res = 0
	for line_raw in f:
		line = line_raw.decode('utf-8')
		hand1_str = line[0:14]
		hand2_str = line[15:29]
		if hand_value(hand1_str) > hand_value(hand2_str):
			res += 1
	return res

Previous Problem Next Problem

Back to Project Euler