Added an haskell solution
This commit is contained in:
parent
de46c0daf2
commit
5f12fd23f2
1 changed files with 36 additions and 0 deletions
36
054.hs
Normal file
36
054.hs
Normal file
|
@ -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
|
Loading…
Reference in a new issue