Created
October 25, 2019 03:00
-
-
Save lumie1337/5eafa658e3b7da19e68b1e6da0ce528a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Arrow | |
import Data.List | |
data Category = Ones | Twos | Threes | Fours | Fives | Sixes | FullHouse | |
| FourOfAKind | LittleStraight | BigStraight | Choice | Yacht | |
deriving (Enum, Eq, Ord, Show) | |
allEqual [] = True | |
allEqual (c:cs) = all (==c) cs | |
counts :: (Eq a, Ord a) => [a] -> [(a, Int)] | |
counts = fmap (head &&& length) . group . sort | |
scoreN n = (* n) . length . filter (== n) | |
straightFrom b = and . zipWith (==) [b..] . sort | |
yacht FullHouse c | [2,3] == (sort $ map snd $ counts c) = sum c | |
yacht FourOfAKind c | not $ null c4 = sum c4 | |
where c4 = [x | (x, l) <- counts c, l >= 4] | |
yacht LittleStraight c | straightFrom 1 c = 30 | |
yacht BigStraight c | straightFrom 2 c = 30 | |
yacht Yacht c | allEqual c = 50 | |
yacht Ones c = scoreN 1 c | |
yacht Twos c = scoreN 2 c | |
yacht Threes c = scoreN 3 c | |
yacht Fours c = scoreN 4 c | |
yacht Fives c = scoreN 5 c | |
yacht Sixes c = scoreN 6 c | |
yacht Choice c = sum c | |
yacht _ _ = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment