1
1

More than 5 years have passed since last update.

# ポーカー、ポーカーの残り＋(2012.6.22、2013.4.19の過去問)

Posted at

``````module Doukaku.Poker (solve) where
import Data.List.Split (splitWhen)
import Data.List (sort, group)

solve :: String -> String
solve = decide . sort . map length . group . sort . parse
where
decide [1,4] = "4K"
decide [2,3] = "FH"
decide [1,1,3] = "3K"
decide [1,2,2] = "2P"
decide [1,1,1,2] = "1P"
decide _ = "--"

parse :: String -> [String]
parse = tail . splitWhen (`elem` "SHDC")
``````

スートは無視してオッケーなので、簡単ですね。`group`関数を使ったお陰で大変シンプルになりました。スートはパース時に捨てています。

そして、二問目は http://qiita.com/Nabetani/items/d819d1e5f2378317511e です。一問目の続編です。

``````module Doukaku.Poker2013 (solve) where
import Data.List (sort, group, tails)
import Data.Char (intToDigit)

solve :: String -> String
solve input
| 'T' `elem` (map fst cards) && 'A' `elem` (map fst cards) &&
samesuit cards == 5 && ordered cards == 5 = "RF"
| samesuit cards == 5 && ordered cards == 5 = "SF"
| samesuit cards == 5 = "FL"
| ordered cards == 5 = "ST"
| any (\cs -> samesuit cs == 4 && ordered cs == 4) card4s = "4SF"
| samesuit cards == 4 = "4F"
| ordered cards == 4 = "4S"
| otherwise = "-"
where
card4s = map (\c -> filter (/= c) cards) cards
cards = parse input
samesuit, ordered :: [(Char, Char)] -> Int
samesuit = maximum . map length . group . sort . map snd
ordered hs = maximum . map (contains (map fst hs)) . tails \$ numberorder
contains _ [] = 0
contains hs (x:xs)
| x `elem` hs = 1 + contains hs xs
| otherwise = 0

parse :: String -> [(Char, Char)]
parse ('1':'0':s:cs) = ('T', s) : parse cs
parse (n:s:cs) = (n, s) : parse cs
parse _ = []

numberorder :: [Char]
numberorder = 'A' : map intToDigit [2 .. 9] ++ "TJQKA"
``````

1
1
0

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
3. You can use dark theme
What you can do with signing up
1
1