Created
January 18, 2023 17:11
-
-
Save akht/693f0cbde58e9a754f641a341c125edf to your computer and use it in GitHub Desktop.
ボウリングのスコア計算プログラム(旧ルール)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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