Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Last active January 2, 2016 13:09
Show Gist options
  • Select an option

  • Save practicingruby/8308306 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/8308306 to your computer and use it in GitHub Desktop.
-module(bowling).
-export([score/1, test/0]).
test() ->
9 = score([{7, 2}]),
% 4 6 8 9 5 0 3 1 0 4
40 = score([{1,3},{2,4},{3,5},{5,4},{2,3},{0,0},{1,2},{1,0},{0,0},{1,3}]),
% 4 6 8 12 5 0 3 1 0 4
43 = score([{1,3},{2,4},{3,5},{5,5},{2,3},{0,0},{1,2},{1,0},{0,0},{1,3}]),
% 4 6 8 15 5 0 3 1 0 4
46 = score([{1,3},{2,4},{3,5},{10},{2,3},{0,0},{1,2},{1,0},{0,0},{1,3}]),
% 4 6 8 14 11 1 3 1 0 4
52 = score([{1,3},{2,4},{3,5},{5,5},{4,6},{1,0},{1,2},{1,0},{0,0},{1,3}]),
% 4 6 8 21 13 3 3 1 0 4
63 = score([{1,3},{2,4},{3,5},{10},{10},{1,2},{1,2},{1,0},{0,0},{1,3}]),
300 = score([{10},{10},{10},{10},{10},{10},{10},{10},{10},{10},{10},{10}]),
% 30 30 30 30 29 20 20 30 30 30
279 = score([{10},{10},{10},{10},{10},{10},{9,1},{10},{10},{10},{10},{10}]),
% 30 30 30 30 29 20 20 29 20 20
258 = score([{10},{10},{10},{10},{10},{10},{9,1},{10},{10},{9,1},{10}]),
% 30 30 30 30 29 20 20 29 20 20
257 = score([{10},{10},{10},{10},{10},{10},{9,1},{10},{10},{9,1},{9,1}]),
ok.
score([]) -> 0;
score([{10}|T]) -> strike(T) + score(T);
score([{A,B}|T]) when 10 =:= A + B -> spare(T) + score(T);
score([{A,B}|T]) -> A + B + score(T).
strike([]) -> 0;
strike([{10}]) -> 0;
strike([{10}, {10}|_]) -> 30;
strike([{10}, {Ball2, _}|_]) -> 20 + Ball2;
strike([{Ball1, Ball2}|_]) -> 10 + Ball1 + Ball2.
spare([]) -> 0;
spare([{10}|_]) -> 20;
spare([{NextBall, _}|_]) -> 10 + NextBall.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment