Skip to content

Instantly share code, notes, and snippets.

@dan5
Created Apr 9, 2011
Embed
What would you like to do?
# 1 ---
def score(pins)
pins.inject(&:+)
end
pins = [1, 4, 2, 8, 5, 0, 10, 0, 0, 4, 5, 5, 2, 0, 6, 1, 10, 0, 10, 5, 5]
p score(pins)
# 2 ---
def strike?(pins, idx)
pins[idx * 2] == 10
end
def spare?(pins, idx)
pins[idx * 2] + pins[idx * 2 + 1] == 10
end
def score_strike_spare(pins)
score = 0
9.times do |i|
next_idx = (i + 1) * 2
if strike?(pins, i)
score += pins[next_idx]
score += pins[next_idx + 1] > 0 ? pins[next_idx + 1] : pins[next_idx + 2]
elsif spare?(pins, i)
score += pins[next_idx]
end
end
score
end
def score(pins)
pins.inject(&:+) + score_strike_spare(pins)
end
pins = [1, 4, 2, 8, 5, 0, 10, 0, 0, 4, 5, 5, 2, 0, 6, 1, 10, 0, 10, 5, 5]
p score(pins)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment