fjord/bowling.rb

33 lines
764 B
Ruby
Raw Normal View History

2024-03-10 22:39:11 +09:00
#!/usr/bin/ruby
# frozen_string_literal: true
2024-03-10 22:39:11 +09:00
score = ARGV[0]
scores = score.split(',')
shots = []
scores.each do |s|
if s == 'X'
shots << 10
shots << 0
else
shots << s.to_i
end
end
frames = shots.each_slice(2).to_a
2024-03-10 22:39:11 +09:00
point = 0
strike = [10, 0]
2024-03-10 22:39:11 +09:00
frames[0..8].each_with_index do |frame, index|
point += frame.sum
# 連続ストライクの場合、2つ先のフレームの1投目も得点となる
point += frames[index + 2].first if frame == strike && frames[index + 1] == strike
point += frames[index + 1].sum if frame == strike
point += frames[index + 1].first if frame.sum == 10 && frame != strike
2024-03-10 22:39:11 +09:00
end
2024-03-21 19:50:34 +09:00
point += frames[9..11].flatten.sum
# point += frames[9..11].sum{|frame| frame.sum} # ご提案頂いた例の回答
2024-03-12 22:51:17 +09:00
puts point