Skip to content

Instantly share code, notes, and snippets.

@5outh
Created July 27, 2012 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5outh/3190326 to your computer and use it in GitHub Desktop.
Save 5outh/3190326 to your computer and use it in GitHub Desktop.
Bowling
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