Last active
October 8, 2023 11:15
-
-
Save hovsater/c3187f2c74f51fd31425f97cf3071ce8 to your computer and use it in GitHub Desktop.
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
def score(rolls) = score_helper(0, 1, rolls.reverse) | |
def score_helper(sum, frame, remaining) | |
case remaining | |
in 10, 10, x if frame == 10 && x <= 10 then sum + 20 + x | |
in 10, x, y if frame == 10 && x + y <= 10 then sum + 10 + x + y | |
in x, y, z if frame == 10 && x + y == 10 && z <= 10 then sum + x + y + z | |
in x, y if frame == 10 && x + y < 10 then sum + x + y | |
in 10, x, y, *rest then score_helper(sum + 10 + x + y, frame + 1, [x, y, *rest]) | |
in x, y, z, *rest if x + y == 10 && z <= 10 then score_helper(sum + 10 + z, frame + 1, [z, *rest]) | |
in x, y, *rest if x + y <= 10 then score_helper(sum + x + y, frame + 1, rest) | |
else | |
raise BowlingError | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment