From 03b23a21ff4fe31a5139f8d3a1935cc6a3fa2b8c Mon Sep 17 00:00:00 2001 From: jepe-w Date: Thu, 29 Feb 2024 13:29:42 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=E3=83=9C=E3=83=BC=E3=83=AA=E3=83=B3?= =?UTF-8?q?=E3=82=B0=E3=83=97=E3=83=AD=E3=82=B0=E3=83=A9=E3=83=A0=E4=BD=9C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.bowling/y-bowling.rb | 59 +++++++++++++++++++++++++++++++++++++++++ bug_cafe | 1 + 2 files changed, 60 insertions(+) create mode 100755 03.bowling/y-bowling.rb create mode 160000 bug_cafe diff --git a/03.bowling/y-bowling.rb b/03.bowling/y-bowling.rb new file mode 100755 index 0000000000..adeef15c5a --- /dev/null +++ b/03.bowling/y-bowling.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +require "debug" + +score = ARGV[0] +score = score.split(',') +shots = [] + +score.each do |s| + #最後の一回は10を一回分ずつ追加 + if shots.length < 18 + if s == 'x' + shots << 10 + shots << 0 + else + shots << s.to_i + end + + else + if s == 'x' + shots << 10 + else + shots << s.to_i + end + + end + +end + +#最後の10回目は場合によっては3回になる +frames = [] +shots.each_slice(2) do |s| + frames << s +end +#2回ずつで分けて最後に一つ残った場合は10回目に追加する +if frames.length == 11 + frames[9] += frames[10] + frames.pop +end + +point = 0 +count = 0 +frames.each do |frame| + if frame.length == 2 + if frame[0] == 10 #ストライクの場合は次のframeの合計を加算 + point += frame.sum + frames[count + 1][0] + frames[count + 1][1] + elsif frame.sum == 10 #スペアの場合は次のframeの一回目の点数を加算 + point += frame.sum + frames[count + 1][0] + else + point += frame.sum + end + + else + point += frame.sum + end + count += 1 +end + + +p point \ No newline at end of file diff --git a/bug_cafe b/bug_cafe new file mode 160000 index 0000000000..20517224d5 --- /dev/null +++ b/bug_cafe @@ -0,0 +1 @@ +Subproject commit 20517224d5b3630774919317268b309a26db6f18 From 3b3702dd2fb431b70bf07b3039aeff89be1c6a0d Mon Sep 17 00:00:00 2001 From: jepe-w Date: Thu, 29 Feb 2024 13:38:44 +0900 Subject: [PATCH 2/7] =?UTF-8?q?require=20debug=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.bowling/y-bowling.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/03.bowling/y-bowling.rb b/03.bowling/y-bowling.rb index adeef15c5a..334694ff44 100755 --- a/03.bowling/y-bowling.rb +++ b/03.bowling/y-bowling.rb @@ -1,5 +1,4 @@ #!/usr/bin/env ruby -require "debug" score = ARGV[0] score = score.split(',') @@ -23,7 +22,7 @@ end end - + end #最後の10回目は場合によっては3回になる @@ -37,13 +36,16 @@ frames.pop end + point = 0 count = 0 + #ストライクの場合は次のframeの合計を加算 + #スペアの場合は次のframeの一回目の点数を加算 frames.each do |frame| if frame.length == 2 - if frame[0] == 10 #ストライクの場合は次のframeの合計を加算 + if frame[0] == 10 point += frame.sum + frames[count + 1][0] + frames[count + 1][1] - elsif frame.sum == 10 #スペアの場合は次のframeの一回目の点数を加算 + elsif frame.sum == 10 point += frame.sum + frames[count + 1][0] else point += frame.sum From 871a54a606c712784674977481e9875c35672691 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Sun, 10 Mar 2024 13:21:39 +0900 Subject: [PATCH 3/7] =?UTF-8?q?=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E3=81=A7=E3=81=AE=E6=8C=87=E6=91=98=E7=AE=87=E6=89=80=E6=94=B9?= =?UTF-8?q?=E5=96=84=E3=80=80v1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.bowling/y-bowling.rb | 51 +++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/03.bowling/y-bowling.rb b/03.bowling/y-bowling.rb index 334694ff44..c003d25f46 100755 --- a/03.bowling/y-bowling.rb +++ b/03.bowling/y-bowling.rb @@ -5,57 +5,48 @@ shots = [] score.each do |s| - #最後の一回は10を一回分ずつ追加 if shots.length < 18 - if s == 'x' + if s == 'X' shots << 10 shots << 0 else shots << s.to_i end - else - if s == 'x' - shots << 10 - else - shots << s.to_i - end - + shots << if s == 'X' + 10 + else + s.to_i + end end - end -#最後の10回目は場合によっては3回になる frames = [] shots.each_slice(2) do |s| frames << s end -#2回ずつで分けて最後に一つ残った場合は10回目に追加する + if frames.length == 11 frames[9] += frames[10] frames.pop end - point = 0 count = 0 - #ストライクの場合は次のframeの合計を加算 - #スペアの場合は次のframeの一回目の点数を加算 frames.each do |frame| - if frame.length == 2 - if frame[0] == 10 - point += frame.sum + frames[count + 1][0] + frames[count + 1][1] - elsif frame.sum == 10 - point += frame.sum + frames[count + 1][0] - else - point += frame.sum - end - - else - point += frame.sum - end + point += if frame.length == 2 && frame[0] == 10 + if frames[count + 1].length == 3 + 10 + frames[count + 1][0] + frames[count + 1][1] + elsif frames[count + 1][0] == 10 + 10 + 10 + frames[count + 2][0] + else + 10 + frames[count + 1][0] + frames[count + 1][1] + end + elsif frame.length == 2 && frame.sum == 10 + frame.sum + frames[count + 1][0] + else + frame.sum + end count += 1 end - - -p point \ No newline at end of file +puts point From 6f95c4c9dac5e6fe591d80ebc946b24eefa51632 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Tue, 12 Mar 2024 08:49:51 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.bowling/y-bowling.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/03.bowling/y-bowling.rb b/03.bowling/y-bowling.rb index c003d25f46..8eb142d403 100755 --- a/03.bowling/y-bowling.rb +++ b/03.bowling/y-bowling.rb @@ -1,4 +1,6 @@ #!/usr/bin/env ruby +# frozen_string_literal: true + score = ARGV[0] score = score.split(',') From da48e78b70627d36ddb295d37c82de17f8c64ede Mon Sep 17 00:00:00 2001 From: jepe-w Date: Tue, 12 Mar 2024 08:52:14 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bug_cafe | 1 - 1 file changed, 1 deletion(-) delete mode 160000 bug_cafe diff --git a/bug_cafe b/bug_cafe deleted file mode 160000 index 20517224d5..0000000000 --- a/bug_cafe +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 20517224d5b3630774919317268b309a26db6f18 From baf15562090b286ff6aa092369952dec9ac996d9 Mon Sep 17 00:00:00 2001 From: jepe-w Date: Tue, 12 Mar 2024 10:33:53 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E3=81=A7=E3=81=AE=E6=8C=87=E6=91=98=E7=AE=87=E6=89=80=E6=94=B9?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.bowling/y-bowling.rb | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/03.bowling/y-bowling.rb b/03.bowling/y-bowling.rb index 8eb142d403..b07aacae70 100755 --- a/03.bowling/y-bowling.rb +++ b/03.bowling/y-bowling.rb @@ -1,12 +1,11 @@ #!/usr/bin/env ruby # frozen_string_literal: true - score = ARGV[0] -score = score.split(',') +split_score = score.split(',') shots = [] -score.each do |s| +split_score.each do |s| if shots.length < 18 if s == 'X' shots << 10 @@ -28,27 +27,27 @@ frames << s end -if frames.length == 11 - frames[9] += frames[10] - frames.pop -end +frames[9] += frames.pop if frames.length == 11 point = 0 -count = 0 -frames.each do |frame| - point += if frame.length == 2 && frame[0] == 10 - if frames[count + 1].length == 3 - 10 + frames[count + 1][0] + frames[count + 1][1] - elsif frames[count + 1][0] == 10 - 10 + 10 + frames[count + 2][0] +frames.each_with_index do |frame, i| + next_frame = frames[i + 1] + not_last_frame = frame.length == 2 + strilke = frame[0] == 10 + spare = frame.sum == 10 + + point += if not_last_frame && strilke + if next_frame.length == 3 + 10 + next_frame[0..1].sum + elsif next_frame[0] == 10 + 10 + 10 + frames[i + 2][0] else - 10 + frames[count + 1][0] + frames[count + 1][1] + 10 + next_frame.sum end - elsif frame.length == 2 && frame.sum == 10 - frame.sum + frames[count + 1][0] + elsif not_last_frame && spare + frame.sum + next_frame[0] else frame.sum end - count += 1 end puts point From 69c20ec7f0e9311c298bcae39d81d23a5f4f690d Mon Sep 17 00:00:00 2001 From: jepe-w Date: Wed, 13 Mar 2024 19:46:39 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E6=8C=87=E6=91=98=E7=AE=87=E6=89=80=E3=81=AE=E6=94=B9=E5=96=84?= =?UTF-8?q?=E3=80=80v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03.bowling/y-bowling.rb | 58 ++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/03.bowling/y-bowling.rb b/03.bowling/y-bowling.rb index b07aacae70..0a8f20a437 100755 --- a/03.bowling/y-bowling.rb +++ b/03.bowling/y-bowling.rb @@ -1,53 +1,35 @@ #!/usr/bin/env ruby # frozen_string_literal: true -score = ARGV[0] -split_score = score.split(',') +scores = ARGV[0].split(',') shots = [] -split_score.each do |s| - if shots.length < 18 - if s == 'X' - shots << 10 - shots << 0 - else - shots << s.to_i - end - else - shots << if s == 'X' - 10 - else - s.to_i - end - end +scores.each do |s| + shots << (s == 'X' ? 10 : s.to_i) + shots << 0 if shots.length < 18 && s == 'X' end -frames = [] -shots.each_slice(2) do |s| - frames << s -end +frames = shots.each_slice(2).to_a frames[9] += frames.pop if frames.length == 11 - -point = 0 -frames.each_with_index do |frame, i| +point = frames.each_with_index.sum do |frame, i| next_frame = frames[i + 1] - not_last_frame = frame.length == 2 + not_last_frame = i != 9 strilke = frame[0] == 10 spare = frame.sum == 10 - point += if not_last_frame && strilke - if next_frame.length == 3 - 10 + next_frame[0..1].sum - elsif next_frame[0] == 10 - 10 + 10 + frames[i + 2][0] - else - 10 + next_frame.sum - end - elsif not_last_frame && spare - frame.sum + next_frame[0] - else - frame.sum - end + if not_last_frame && strilke + if next_frame.length == 3 + 10 + next_frame[0..1].sum + elsif next_frame[0] == 10 + 10 + 10 + frames[i + 2][0] + else + 10 + next_frame.sum + end + elsif not_last_frame && spare + frame.sum + next_frame[0] + else + frame.sum + end end puts point