Created
July 27, 2012 20:26
-
-
Save 5outh/3190326 to your computer and use it in GitHub Desktop.
Bowling
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.Applicative | |
import Control.Arrow | |
game = scanl1 (+) . getFrameScores . getFrames | |
where | |
getFrames xs = if length scores == 10 then scores | |
else (take 9 scores) ++ [concat $ drop 9 scores] | |
where | |
scores = parseScores xs | |
parseScores [] = [] | |
parseScores xs = case head xs of | |
10 -> [10] : (parseScores $ tail xs) | |
_ -> (take 2 xs) : (parseScores $ drop 2 xs) | |
getFrameScores (x:[]) = [sum x] | |
getFrameScores (x:xs) = case length x of | |
1 -> case length next of | |
1 -> sum (head <$> [x, next, nnext]) : getFrameScores xs | |
_ -> head x + (sum $ take 2 next) : getFrameScores xs | |
where (next, nnext) = head &&& last $ take 2 xs | |
2 -> case sum x of | |
10 -> sum x + next : getFrameScores xs | |
_ -> sum x : getFrameScores xs | |
where next = head $ head xs | |
_ -> error "Parse error on frame. More than two turns." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment