// you’re reading...
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Project Euler Solutions

Project Euler Problem 54 Solution

Problem Description

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?

Analysis

This is one problem we never cared for and left it languishing in mediorcrity after spitting out the answer. I hope this saves someone some time because it’s not at all enjoyable.

Solution

Runs < 1 second.

my ($flush, $straight, $score, $p1, $p2, @hand, @rank, %h);
my @rating = (0,7,3,1,6,2,4,5,8,9 ); #('high card','four of a kind','three of a kind','pair','full house','two pairs','straight','flush','straight flush','royal flush');
my %cv = (A => 14, K => 13, Q => 12, J => 11, T => 10); #card values for ace & face cards
 
my $trials = <DATA>; #data at end of script
my $player1 = 0;
 
for (1..$trials) {
  @cards = split /s/,<DATA>;  #separate cards into an array
  ($r1,$h1,$px1) = rank(splice(@cards,0,5));
  ($r2,$h2,$px2) = rank(splice(@cards,0,5));
  $player1++ if $r1>$r2;
  if ($r1==$r2) {
    if ($r1==0 || $r1==4 || $r1==5 || $r1==8) {$player1++ if $h1>$h2}
      else {$player1++ if $px1>$px2}
  } 
}
 
print "Player 1 wins $player1 hands.";
 
sub rank {
  %h=(); $score = 0; $highset=0;
  @hand = @_;
  $flush = flush_check(map (chop,@hand));
 
  @rank = sort{$b <=> $a} map ($cv{$_} || $_, @hand);
  $straight = straight_check(@rank);
  $h{$_}++ foreach @rank;
  ($p1,$p2) = reverse sort values %h;
  if ($flush || $straight) {
    $score = 5 + $straight + $flush*2;
    $score++ if $flush && $rank[-1]==10;
  } elsif ($p1>1) { $score = $p2*2-$p1+3 }
  if ($p1>1) {
    for (sort keys %h)  { if ($h{$_}==$p1) {$highset=$_;last}
  }
}
return ($rating[$score],$rank[0],$highset);
}
 
sub flush_check {
  return 1 if $_[0] eq $_[1] && $_[1] eq $_[2] && $_[2] eq $_[3] && $_[3] eq $_[4];
  return 0
}
 
sub straight_check {		
  return 1 if $_[0]==14 && $_[1]==5 && $_[2]==4 && $_[3]==3 && $_[4]==2;
  return 1 if $_[0]-$_[1]==1 && $_[1]-$_[2]==1 && $_[2]-$_[3]==1 && $_[3]-$_[4]==1;
  return 0
}
__END__
1000
8C TS KC 9H 4S 7D 2S 5D 3S AC
5C AD 5D AC 9C 7C 5H 8D TD KS
3H 7H 6S KC JS QH TD JC 2D 8S
TH 8H 5C QS TC 9H 4D JC KS JS
7C 5H KC QH JD AS KH 4C AD 4S
5H KS 9C 7D 9H 8D 3S 5D 5C AH
6H 4H 5C 3H 2H 3S QH 5S 6S AS
TD 8C 4H 7C TC KC 4C 3H 7S KS
7C 9C 6D KD 3H 4C QS QC AC KH
JC 6S 5H 2H 2D KD 9D 7C AS JS
AD QH TH 9D 8H TS 6D 3S AS AC
        ... data continues ...

Comments

None.

Discussion

4 Responses to “Project Euler Problem 54 Solution”

  1. I have seen this problem in the past but I have never seen such a creative solution. Thanks for sharing this with others.

    TJ

    Posted by TJ Mathews | July 3, 2009, 2:39 PM
  2. Great solution.
    I think, for flush and straight, if you have sorted the values, you can just compare the first and the last.
    Also, with sorted values for four of a kind you can compare 1 and 4 and also 2 and 5 and one has to be true.

    Posted by MK | September 24, 2009, 10:40 PM

Trackbacks/Pingbacks

  1. [...] I would like to learn how to make prettier code. One very short solution I found was the one at dream shire written in Perl.The photo is taken by images_of_money and kindly shared under the Creative Commons [...]

Post a comment