Last active
January 31, 2022 19:39
-
-
Save gadtfly/9d395f366b9567442b6b64a271c1b3f4 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 Data.List | |
| import Control.Monad | |
| cards = [4,5,5,6,6] | |
| score :: (Num a, Eq a) => [a] -> Int | |
| score cards = fifteenScore + pairScore + runScore | |
| where | |
| fifteenScore = 2 * (length (allFifteens cards)) | |
| pairScore = 2 * (length (allPairs cards)) | |
| runScore = sum (map length (allRuns cards)) | |
| allFifteens :: (Num a, Eq a) => [a] -> [[a]] | |
| allFifteens = filter isFifteen . subsequences | |
| isFifteen :: (Num a, Eq a) => [a] -> Bool | |
| isFifteen = (== 15) . sum | |
| allPairs :: (Eq a) => [a] -> [[a]] | |
| allPairs = filter isPair . subsequences | |
| isPair :: Eq a => [a] -> Bool | |
| isPair [x, y] = x == y | |
| isPair otherwise = False | |
| allRuns :: (Num a, Eq a) => [a] -> [[a]] | |
| allRuns = onlyLongestSubsequences . filter isRun . filter ((> 2) . length) . subsequences | |
| onlyLongestSubsequences :: Eq a => [[a]] -> [[a]] | |
| onlyLongestSubsequences xss = filter (not . anyProperSubsequences xss) xss | |
| anyProperSubsequences :: Eq a => [[a]] -> [a] -> Bool | |
| anyProperSubsequences xss xs = any (isProperSubsequenceOf xs) xss | |
| isProperSubsequenceOf :: Eq a => [a] -> [a] -> Bool | |
| isProperSubsequenceOf xs ys = isSubsequenceOf xs ys && length xs < length ys | |
| isRun :: (Num a, Eq a) => [a] -> Bool | |
| isRun = all (== -1) . forwardDifference | |
| forwardDifference :: Num a => [a] -> [a] | |
| forwardDifference = ap (zipWith (-)) tail | |
| main :: IO () | |
| main = print $ score cards |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment