Project Euler

Project Euler Problem 54

steloflute 2012. 6. 9. 11:34

Problem 54

10 October 2003

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?


Answer:
376



Solution in C#


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;


namespace Euler {

    class Problem54 {

        static Dictionary<string, int> cardValue = new Dictionary<string, int>();


        static int number(string s) {

            return cardValue[s.Substring(0, s.Length - 1)];

        }


        static string suit(string s) {

            return s.Substring(s.Length - 1, 1);

        }


        static int rank(string[] hands) {

            foreach (var h in hands) Console.Write(h + " ");


            var nums = hands.Select(number).ToArray();

            Array.Sort(nums);

            var numsSet = new HashSet<int>(nums);

            var suits = hands.Select(suit).ToArray();

            var suitsSet = new HashSet<string>(suits);

            

            var highCardPair = 0;            

            var highCardTK = 0;

            var highCardFK = 0;

            

            //900 Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

            if (nums[0] == 10 && nums[4] == 14 && suitsSet.Count == 1) {

                Console.WriteLine("Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.");

                return 900;

            }

            

            //800 Straight Flush: All cards are consecutive values of same suit.

            if (nums[4] - nums[0] == 4 && suitsSet.Count == 1) {

                Console.WriteLine("Straight Flush: All cards are consecutive values of same suit.");

                return 800 + nums[4];

            }

            

            //700 Four of a Kind: Four cards of the same value.

            bool fk = false;

            foreach (var ns in numsSet) {

                if (nums.Count(x => x == ns) == 4) {

                    fk = true;

                    if (ns > highCardFK) highCardFK = ns;

                    break;

                }

            }

            if (fk) {

                Console.WriteLine("Four of a Kind: Four cards of the same value.");

                return 700 + highCardFK;

            }


            //600 Full House: Three of a kind and a pair.

            bool tk = false;

            foreach (var ns in numsSet) {

                if (nums.Count(x => x == ns) == 3) {

                    tk = true;

                    if (ns > highCardTK) highCardTK = ns;

                    break;

                }

            }

            int numPairs = 0;

            foreach (var ns in numsSet) {

                if (nums.Count(x => x == ns) == 2) {

                    numPairs++;

                    if (ns > highCardPair) highCardPair = ns;

                }

            }

            if (tk && numPairs == 1) {

                Console.WriteLine("Full House: Three of a kind and a pair.");

                return 600 + highCardTK;

            }


            //500 Flush: All cards of the same suit.

            if (suitsSet.Count == 1) {

                Console.WriteLine("Flush: All cards of the same suit.");

                return 500 + nums[4];

            }


            //400 Straight: All cards are consecutive values.

            if (nums[4] - nums[0] == 4 && numsSet.Count == 5) {

                Console.WriteLine("Straight: All cards are consecutive values.");

                return 400 + nums[4];

            }

            

            //300 Three of a Kind: Three cards of the same value.

            if (tk) {

                Console.WriteLine("Three of a Kind: Three cards of the same value.");

                return 300 + highCardTK;

            }


            //200 Two Pairs: Two different pairs.

            if (numPairs == 2) {

                Console.WriteLine("Two Pairs: Two different pairs.");

                return 200 + highCardPair;

            }


            //100 One Pair: Two cards of the same value.

            if (numPairs == 1) {

                Console.WriteLine("One Pair: Two cards of the same value.");

                return 100 + highCardPair;

            }

            

            //000 High Card: Highest value card.

            Console.WriteLine("High Card: Highest value card.");

            return nums[4];

        }


        static bool player1HighestCard(string[] hands1, string[] hands2) {

            var nums1 = new HashSet<int>(hands1.Select(number));

            var nums2 = new HashSet<int>(hands2.Select(number));

            nums1.ExceptWith(nums2);

            if (nums1.Count == 0) return false;

            return nums1.Max() > nums2.Max();

        }


        static bool player1Win(string[] hands1, string[] hands2) {

            var r1 = rank(hands1);

            var r2 = rank(hands2);


            Console.WriteLine("{0} {1}", r1, r2);

            if (r1 == r2) return player1HighestCard(hands1, hands2);

            if (r1 > r2) return true;

            return false;

        }


        public static void run() {

            var allLines = File.ReadAllLines("poker.txt");

            for (var i = 2; i <= 9; i++)

                cardValue[i.ToString()] = i;

            cardValue["T"] = 10;

            cardValue["J"] = 11;

            cardValue["Q"] = 12;

            cardValue["K"] = 13;

            cardValue["A"] = 14;


            var hands1 = new string[5];

            var hands2 = new string[5];

            var numPlayer1Win = 0;

            foreach (var c in allLines) {

                //Console.WriteLine(c);                

                var a = c.Split(' ');

                hands1 = a.Take(5).ToArray();

                hands2 = a.Skip(5).Take(5).ToArray();

                if (player1Win(hands1, hands2)) numPlayer1Win++;

                //Console.ReadKey();

            }

            Console.WriteLine(numPlayer1Win);

        }

    }

}


'Project Euler' 카테고리의 다른 글

Project Euler Problem 61  (0) 2012.10.04
(Haskell) Project Euler Solutions  (0) 2012.06.12
Project Euler Problem 52  (0) 2012.06.09
Project Euler Problem 51  (0) 2012.06.09
Project Euler Problem 50  (0) 2012.06.09