Skip to content

Instantly share code, notes, and snippets.

@dan5
Created April 10, 2011 01:07
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 dan5/911950 to your computer and use it in GitHub Desktop.
Save dan5/911950 to your computer and use it in GitHub Desktop.
def score(pins)
frames = pins.join(',').scan(/(\d+),?(\d+)?/).map{|e| e.map{|i| i.to_i}}
ss = frames.map{|s1, s2| s1 == 10 ? :strike : (s1 + s2 == 10 ? :spare : nil)}
sum = frames.size > 10 ? frames.pop[0] : 0
frames.zip(ss.unshift(nil)).zip(ss.unshift(nil)).map{|e1, e2| [e1[0], [e1[1], e2]]}.each do |score, hist|
sum += score[0] if hist[0] == :strike and hist[1] == :strike
case hist[0]
when :strike
sum += score[0] + score[1]
when :spare
sum += score[0]
end
sum += score[0] + score[1]
end
sum
end
def bowl(n)
@pins ||= []
@pins << n
score(@pins)
end
pins = [1, 4, 2, 8, 5, 0, 10, 0, 0, 4, 5, 5, 2, 0, 6, 1, 10, 0, 10, 5, 5]
pins.each do |n|
puts sprintf("%2d %d\n", n, bowl(n))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment