From 24463dcf8b560e1f7dc442357f2f4d7f02f9e555 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Tue, 31 Jan 2012 23:51:51 -0500 Subject: [PATCH] Haskell 054 --- haskell/e054.hs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 haskell/e054.hs diff --git a/haskell/e054.hs b/haskell/e054.hs new file mode 100644 index 0000000..b44d3b1 --- /dev/null +++ b/haskell/e054.hs @@ -0,0 +1,58 @@ +{- How many hands did player one win in the game of poker? + + 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 2C 3S 8S 8D TD Player 2 + Pair of Fives Pair of Eights + + 2 5D 8C 9S JS AC 2C 5C 7D 8S QH + Highest card Ace Highest Card Queen Player 1 + 3 2D 9C AS AH AC 3D 6D 7D TD QD + Three Aces Flush with Diamonds Player 2 + + 4 4D 6S 9H QH QC 3D 6D 7H QD QS Player 1 + Pair of Queens Pair of Queens + Highest card Nine Highest card Seven + + 5 2H 2D 4C 4D 4S 3C 3D 3S 9S 9D Player 1 + Full House Full House + With Three Fours With Three Threes + + 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? +-} + +import Util.Poker +import Text.Printf + +play :: (Hand, Hand) -> (Int, Int) +play (p1, p2) + | (rankHand p1) > (rankHand p2) = (1, 0) + | otherwise = (0, 1) + +main = do + pokerData <- readFile "../python/p054/poker.txt" + let l = (words . head . lines) pokerData + let games = map ((splitAt 5) . (map (parseCard)) . words) $ lines pokerData + let score = foldl (\x y -> (fst x + fst y, snd x + snd y)) (0, 0) $ map (play) games + printf "Player One won %d times\n" (fst score) + printf "Player Two won %d times\n" (snd score)