From 5f12fd23f2052ea47325b6ca63c9cc135d47c56b Mon Sep 17 00:00:00 2001 From: "Yann Esposito (Yogsototh)" Date: Fri, 25 Nov 2011 16:58:48 +0100 Subject: [PATCH] Added an haskell solution --- 054.hs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 054.hs diff --git a/054.hs b/054.hs new file mode 100644 index 0000000..36f8e48 --- /dev/null +++ b/054.hs @@ -0,0 +1,36 @@ +import Data.List +import Data.Maybe +import Control.Monad + +readCard [r,s]=(parseRank r, parseSuit s) + where parseSuit = translate "SHDC" + parseRank = translate "23456789TJQKA" + translate from x = fromJust $ elemIndex x from + +solveHand hand = (handRank,tiebreak) + where handRank + | flush && straight = 9 + | hasKinds 4 = 8 + | all hasKinds [2,3] = 7 + | flush = 6 + | straight = 5 + | hasKinds 3 = 4 + | 1 < length (kind 2) = 3 + | hasKinds 2 = 2 + | otherwise = 1 + tiebreak = kind =<< [4,3,2,1] + hasKinds = not . null . kind + kind n = map head $ filter ((n==).length) $ group ranks + ranks = sortBy (flip compare) $ map fst hand + flush = 1 == length (nub (map snd hand)) + straight = length (kind 1) == 5 && 4 == head ranks - last ranks + +gameLineToHands = splitAt 5 . map readCard . words +plwon (a,b) = solveHand a > solveHand b + +main = do + f <- readFile "poker.txt" + let games = map gameLineToHands $ lines f + wins = filter plwon games + mapM print (map plwon games) + print $ length wins