Created
December 14, 2011 15:23
-
-
Save snoozer05/1477002 to your computer and use it in GitHub Desktop.
sumim さんのボウリング集計のコード(http://d.hatena.ne.jp/sumim/20111214/p1)を Ruby で
This file contains hidden or 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
def score1(pins) | |
cursor = 0 | |
frame_points = [] | |
9.times do | |
frame_points << pins[cursor, 3] | |
cursor += 2 | |
if frame_points.last.first == 10 | |
cursor -= 1 | |
else | |
if frame_points.last.take(2).inject(:+) != 10 | |
frame_points.last.delete_at(-1) | |
end | |
end | |
end | |
frame_points << pins[cursor..-1] | |
frame_points.map{|points| points.inject(:+)}.inject(:+) | |
end | |
score1 [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] #=> 300 | |
score1 [10, 10, 10, 10, 10, 10, 10, 10, 10, 3, 3] #=> 255 | |
score1 [0, 0, 10, 8, 2, 10, 10, 10, 5, 3, 8, 2, 10, 2, 3] #=> 161 | |
score1 [5, 3, 7, 2, 8, 2, 10, 7, 1, 9, 0, 6, 2, 10, 6, 4, 8, 0] #=> 126 | |
def score2(frames) | |
rolls = Hash.new{|h, k| h[k] = Array.new} | |
n_roll = -1 | |
frame_points = frames.map.with_index do|frame_pins, idx| | |
frame_pins.each do |pin| | |
n_roll += 1 | |
rolls[n_roll][0] = pin | |
end | |
frame_point = [frame_pins.inject(:+)] | |
if idx < 10 && frame_point.first == 10 | |
(1..(3-frame_pins.size)).each do |delta| | |
frame_point << rolls[n_roll+delta] | |
end | |
end | |
frame_point | |
end | |
frame_points.map{|points| frame_point.flatten.inject(&:+)}.flatten.inject(&:+) | |
end | |
score2 [[10], [10], [10], [10], [10], [10], [10], [10], [10], [10], [10], [10]] #=> 300 | |
score2 [[10], [10], [10], [10], [10], [10], [10], [10], [10], [3, 3]] #=> 255 | |
score2 [[0, 0], [10], [8, 2], [10], [10], [10], [5, 3], [8, 2], [10], [2, 3]] #=> 161 | |
score2 [[5, 3], [7, 2], [8, 2], [10], [7, 1], [9, 0], [6, 2], [10], [6, 4], [8, 0]] #=> 126 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment