Skip to content

Instantly share code, notes, and snippets.

@akht
Created January 18, 2023 17:11
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 akht/693f0cbde58e9a754f641a341c125edf to your computer and use it in GitHub Desktop.
Save akht/693f0cbde58e9a754f641a341c125edf to your computer and use it in GitHub Desktop.
ボウリングのスコア計算プログラム(旧ルール)
scores = ARGV[0].strip.split(',').flat_map { |p| p == 'X' ? [10, nil] : p.to_i }
frames = scores.each_slice(6).flat_map { |n| n.size.even? ? n.each_slice(2).to_a : [n] }
total = (0..9).inject(0) do |acc, i|
current_frame, next_frame, after_next_frame = frames.slice(i, 3)
bonus = [current_frame.first == 10, current_frame.compact.sum == 10].sum { |i| i ? 1 : 0 } # strikeは2投分,spareは1投分
acc + current_frame.compact.sum + [next_frame, after_next_frame].flatten.compact.take(bonus).sum
end
puts total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment