mirror of
https://github.com/correl/euler.git
synced 2024-11-27 11:09:54 +00:00
Merge branch 'haskell/054'
This commit is contained in:
commit
6d55d96cb2
3 changed files with 190 additions and 0 deletions
106
haskell/Util/Poker.hs
Normal file
106
haskell/Util/Poker.hs
Normal file
|
@ -0,0 +1,106 @@
|
|||
module Util.Poker where
|
||||
|
||||
import Data.Char (digitToInt)
|
||||
import Data.List (sort, sortBy, group)
|
||||
import Data.Ord (comparing)
|
||||
|
||||
data Suit = Hearts | Diamonds | Clubs | Spades
|
||||
deriving (Show, Eq, Ord)
|
||||
|
||||
data Card = Card { cardValue :: Int
|
||||
, cardSuit :: Suit
|
||||
}
|
||||
deriving (Show)
|
||||
|
||||
instance Eq Card where
|
||||
x == y = (cardValue x) == (cardValue y)
|
||||
x /= y = (cardValue x) /= (cardValue y)
|
||||
|
||||
instance Ord Card where
|
||||
compare x y = compare (cardValue x) (cardValue y)
|
||||
|
||||
parseCard :: String -> Card
|
||||
parseCard s = parseCard' $ break (\x -> notElem x ('A':'K':'Q':'J':'T':['0'..'9'])) s
|
||||
|
||||
parseCard' :: (String, String) -> Card
|
||||
parseCard' (v, s) = Card v' s'
|
||||
where v' = case v of
|
||||
"T" -> 10
|
||||
"J" -> 11
|
||||
"Q" -> 12
|
||||
"K" -> 13
|
||||
"A" -> 14
|
||||
_ -> foldl (\x y -> x * 10 + y) 0 $ map (digitToInt) v
|
||||
s' = case s of
|
||||
"H" -> Hearts
|
||||
"D" -> Diamonds
|
||||
"C" -> Clubs
|
||||
"S" -> Spades
|
||||
_ -> error "Unknown card suit"
|
||||
|
||||
type Hand = [Card]
|
||||
|
||||
data HandRank = HighCard
|
||||
| OnePair
|
||||
| TwoPair
|
||||
| ThreeOfAKind
|
||||
| Straight
|
||||
| Flush
|
||||
| FullHouse
|
||||
| FourOfAKind
|
||||
| StraightFlush
|
||||
| RoyalFlush
|
||||
deriving(Show, Eq, Ord)
|
||||
|
||||
type HandValue = (HandRank, [Int])
|
||||
|
||||
rankHand :: Hand -> HandValue
|
||||
rankHand h
|
||||
| biggestSet > 1 = rankSets grouped
|
||||
| isFlush h && isStraight h = (StraightFlush, cardValues $ fixStraight h')
|
||||
| isFlush h = (Flush, cardValues h')
|
||||
| isStraight h = (Straight, cardValues $ fixStraight h')
|
||||
| otherwise = (HighCard, cardValues h')
|
||||
where h' = take 5 $ reverse $ sort h
|
||||
grouped = sets $ take 5 h
|
||||
biggestSet = length $ head grouped
|
||||
|
||||
rankSets :: [[Card]] -> HandValue
|
||||
rankSets (x:xs) = case (length x) of
|
||||
4 -> (FourOfAKind, values)
|
||||
1 -> (HighCard, cardValues $ concat (x:xs))
|
||||
_ -> case rest of
|
||||
(OnePair, values') -> case l of 3 -> (FullHouse, (cardValue $ head x) : values')
|
||||
2 -> (TwoPair, (cardValue $ head x) : values')
|
||||
_ -> (OnePair, values)
|
||||
_ -> case l of 3 -> (ThreeOfAKind, values)
|
||||
_ -> (OnePair, values)
|
||||
where l = length x
|
||||
rest = rankSets $ filter (\x' -> length x' <= l) xs
|
||||
values = (cardValue $ head x) : (take 1 $ cardValues $ concat xs)
|
||||
rankSets [] = (HighCard, [])
|
||||
|
||||
cardValues :: [Card] -> [Int]
|
||||
cardValues = map (cardValue)
|
||||
|
||||
sets :: Hand -> [[Card]]
|
||||
sets h = reverse $ sortBy (comparing (\x -> length x)) $ group $ sort h
|
||||
|
||||
isFlush :: Hand -> Bool
|
||||
isFlush h = suits == 5
|
||||
where groups = reverse $ sortBy (comparing (\x -> length x)) $ group $ sort $ map (cardSuit) h
|
||||
suits = length $ head groups
|
||||
|
||||
isStraight :: Hand -> Bool
|
||||
isStraight h = isStraight' $ sort $ cardValues h
|
||||
|
||||
isStraight' :: [Int] -> Bool
|
||||
isStraight' [] = False
|
||||
isStraight' (2:3:4:5:14:[]) = True
|
||||
isStraight' xs = xs == [(head xs) .. (last xs)]
|
||||
|
||||
fixStraight :: Hand -> Hand
|
||||
fixStraight (x:xs)
|
||||
| 2 `elem` values && 14 `elem` values = xs ++ [(Card 1 (cardSuit x))]
|
||||
| otherwise = (x:xs)
|
||||
where values = cardValues (x:xs)
|
26
haskell/Util/TestPoker.hs
Normal file
26
haskell/Util/TestPoker.hs
Normal file
|
@ -0,0 +1,26 @@
|
|||
import Test.HUnit
|
||||
import Util.Poker
|
||||
|
||||
testFour = [parseCard "9D", parseCard "2H", parseCard "2S", parseCard "2D", parseCard "2C"]
|
||||
testThree = [parseCard "9D", parseCard "2H", parseCard "5S", parseCard "2D", parseCard "2C"]
|
||||
testFull = [parseCard "9D", parseCard "2H", parseCard "9S", parseCard "2D", parseCard "2C"]
|
||||
testTwo = [parseCard "9D", parseCard "2H", parseCard "9S", parseCard "2D", parseCard "5C"]
|
||||
testFlush = [parseCard "9D", parseCard "2D", parseCard "KD", parseCard "JD", parseCard "5D"]
|
||||
testStraight = [parseCard "9D", parseCard "TS", parseCard "JD", parseCard "QD", parseCard "KD"]
|
||||
testWheel = [parseCard "2D", parseCard "3S", parseCard "4D", parseCard "5D", parseCard "AD"]
|
||||
testHigh1 = [parseCard "8C", parseCard "TS", parseCard "KC", parseCard "9H", parseCard "4S"]
|
||||
testHigh2 = [parseCard "7D", parseCard "2S", parseCard "5D", parseCard "3S", parseCard "AC"]
|
||||
|
||||
tests = TestList [ "Rank 4 of a kind" ~: FourOfAKind ~=? (fst $ rankHand testFour)
|
||||
, "Rank 3 of a kind" ~: ThreeOfAKind ~=? (fst $ rankHand testThree)
|
||||
, "Rank a full house" ~: FullHouse ~=? (fst $ rankHand testFull)
|
||||
, "Rank two pair" ~: TwoPair ~=? (fst $ rankHand testTwo)
|
||||
, "Rank a flush" ~: Flush ~=? (fst $ rankHand testFlush)
|
||||
, "Rank a straight" ~: Straight ~=? (fst $ rankHand testStraight)
|
||||
, "Rank the wheel" ~: Straight ~=? (fst $ rankHand testWheel)
|
||||
, "Rank a high card" ~: HighCard ~=? (fst $ rankHand testHigh1)
|
||||
|
||||
, "Compare values" ~: True ~=? (rankHand testHigh1) < (rankHand testHigh2)
|
||||
]
|
||||
|
||||
main = runTestTT tests
|
58
haskell/e054.hs
Normal file
58
haskell/e054.hs
Normal file
|
@ -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)
|
Loading…
Reference in a new issue