Last active
December 18, 2015 07:59
-
-
Save rickardlindberg/5750566 to your computer and use it in GitHub Desktop.
Bowling kata with kajgo 10 Jun 2013.
This file contains 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
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