Skip to content

Instantly share code, notes, and snippets.

@rickardlindberg
Last active December 18, 2015 07:59
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 rickardlindberg/5750566 to your computer and use it in GitHub Desktop.
Save rickardlindberg/5750566 to your computer and use it in GitHub Desktop.
Bowling kata with kajgo 10 Jun 2013.
main = do
print $ score (replicate 20 0) == 0
print $ score (replicate 20 1) == 20
print $ score ([4,6,1,0] ++ replicate 16 0) == 10 + 1 + 1
print $ score ([10,6,1] ++ replicate 16 0) == 10 + 6 + 1 + 6 + 1
print $ score (replicate 18 0 ++ [3,7,1]) == 11
print $ score (replicate 18 0 ++ [10,5,2]) == 10 + 5 + 2
print $ score (replicate 18 0 ++ [10,10,10]) == 30
print $ score (replicate 12 10) == 300
score :: [Int] -> Int
score rolls = score' 10 rolls
where
score' :: Int -> [Int] -> Int
score' framesLeft xs
| framesLeft == 0 = 0
| isStrike xs = strikeScore (drop 1 xs) + score' (framesLeft - 1) (drop 1 xs)
| isSpare xs = spareScore (drop 2 xs) + score' (framesLeft - 1) (drop 2 xs)
| otherwise = sum (take 2 xs) + score' (framesLeft - 1) (drop 2 xs)
isStrike :: [Int] -> Bool
isStrike rolls = head rolls == 10
isSpare :: [Int] -> Bool
isSpare rolls = head rolls + head (tail rolls) == 10
strikeScore :: [Int] -> Int
strikeScore nextRolls = 10 + head nextRolls + head (tail nextRolls)
spareScore :: [Int] -> Int
spareScore nextRolls = 10 + head nextRolls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment