Project Euler

Problem #54

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?

Ruby: Running time = 0.13s
+#Enumerable

def straight?(vals)
  v=vals.sort
  v==((v.first...(5+v.first)).to_a)
end

def flush?(suits)
  suits.uniq.length==1
end

def hand_eval(hand)
  suits=hand.map{|i|i.last}
  vals=hand.map{|i|i.first}
  return :straight_flush if flush?(suits) and straight?(vals)
  u=vals.uniq
  return :four if u.length==2 and (vals.count(u.first))%3==1
  return :full_house if u.length==2
  return :flush if flush?(suits)
  return :straight if straight?(vals)
  return :three if u.length==3 and u.any?{|uu|vals.count(uu)==3}
  return :two_pair if u.length==3
  return :pair if u.length==4
  :high
end

$p54handValues={:high=>1,
  	        :pair=>2,
	        :two_pair=>3,
	        :three=>4,
	        :straight=>5,
	        :flush=>6,
	        :full_house=>7,
	        :four=>8,
	        :straight_flush=>9}

def high_card(h1,h2)
  h1.sort!
  h2.sort!
  while h1.last==h2.last
    h1.pop
    h2.pop
  end
  h1.last<=>h2.last
end

def freq(a)
  a.sort{|u,v|a.count(u)<=>a.count(v)}.last
end

def game_eval(game)
  game.map!{|h|[hand_eval(h),h]}
  a=$p54handValues[game.first.first]<=>$p54handValues[game.last.first]
  return a unless a==0
  h1=game.first.last.map{|p|p.first}
  h2=game.last.last.map{|p|p.first}
  case game.first.first
    when :high then
      high_card(h1,h2)
    when :pair then
      r=(freq(h1)<=>freq(h2))
      return r unless r==0
      high_card(h1,h2)
    when :two_pair then
      hh1=h1.sort.last
      hh1=h1.sort[-2] if h1.count(hh1)==1
      hh2=h2.sort.last
      hh2=h2.sort[-2] if h2.count(hh2)==1
      r=hh1<=>hh2
      if(r==0)
	r=freq(h1.reject{|i|i==hh1})<=>freq(h2.reject{|i|i==hh2})
	r=high_card(h1,h2) if r==0
      end
      r
    when :three then
      r=(freq(h1)<=>freq(h2))
      return r unless r==0
      high_card(h1,h2)
    when :straight then
      high_card(h1,h2)
    when :flush then
      high_card(h1,h2)
    when :full_house then
      r=(freq(h1)<=>freq(h2))
      if(r==0)
	r=h1.sum<=>h2.sum
      end
      r
    when :four then
      r=(freq(h1)<=>freq(h2))
      return r unless r==0
      high_card(h1,h2)
    when :straight_flush then
      high_card(h1,h2)
  end
end

def p54
  games=File.read("poker.txt").split("\n").map do |l|
  	l.split(" ").map do |h|
		n=h[0..0]
		[n.to_i==0 ? {"T"=>10,"J"=>11,"Q"=>12,"K"=>13,"A"=>14}[n] : n.to_i,h[1..1].to_sym]
	end
  end
  games.map!{|h|[h[0..4],h[5..9]]}
  games.map!{|game|game_eval(game)}
  puts games.select{|g|g==1}.length
end