Skip to content

Instantly share code, notes, and snippets.

@hovsater
Last active October 8, 2023 11:15
Show Gist options
  • Save hovsater/c3187f2c74f51fd31425f97cf3071ce8 to your computer and use it in GitHub Desktop.
Save hovsater/c3187f2c74f51fd31425f97cf3071ce8 to your computer and use it in GitHub Desktop.
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