2024-03-10 22:39:11 +09:00
|
|
|
#!/usr/bin/ruby
|
2024-03-13 20:37:35 +09:00
|
|
|
# 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
|
|
|
|
|
2024-03-20 13:22:12 +09:00
|
|
|
frames = shots.each_slice(2).to_a
|
2024-03-10 22:39:11 +09:00
|
|
|
|
|
|
|
point = 0
|
2024-03-20 13:22:12 +09:00
|
|
|
strike = [10, 0]
|
2024-03-10 22:39:11 +09:00
|
|
|
|
2024-03-20 13:22:12 +09:00
|
|
|
frames[0..8].each_with_index do |frame, index|
|
2024-03-11 22:58:28 +09:00
|
|
|
point += frame.sum
|
2024-03-13 20:37:35 +09:00
|
|
|
# 連続ストライクの場合、2つ先のフレームの1投目も得点となる
|
2024-03-20 13:22:12 +09:00
|
|
|
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
|
|
|
|
2024-03-20 13:22:12 +09:00
|
|
|
puts point
|