From 437f53b3f94b9833bf1a8c0cf0bdefcae0599acc Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Tue, 29 Aug 2023 14:22:07 +0900 Subject: [PATCH 1/3] added calendar code --- 02.calendar/calendar.rb | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 02.calendar/calendar.rb diff --git a/02.calendar/calendar.rb b/02.calendar/calendar.rb new file mode 100644 index 0000000000..d7cdcc7be6 --- /dev/null +++ b/02.calendar/calendar.rb @@ -0,0 +1,46 @@ +require 'date' +require 'optparse' + +# コマンドラインでparseする +options = {} +OptionParser.new do |opts| + opts.banner = "Usage: ruby calendar.rb [options]" + + opts.on("-m", "--month MONTH", Integer, "Specify the month (1-12)") do |month| + options[:month] = month + end + + opts.on("-y", "--year YEAR", Integer, "Specify the year") do |year| + options[:year] = year + end +end.parse! + +# 今の日付を取る +current_date = Date.today +year = options[:year] || current_date.year +month = options[:month] || current_date.month + +# date enumeratorを作る +date_range = Date.new(year, month, 1)..Date.new(year, month, -1) +dates_enum = date_range.to_enum + +# カレンダーを表示する +puts "\n#{Date::MONTHNAMES[month]} #{year}".center(20) +puts "日 月 火 水 木 金 土" + +# 最初の日に計算する +initial_offset = dates_enum.peek.wday + +# 見やすいのため +print " " * initial_offset + +# 1ヶ月分表示する +dates_enum.each do |date| + print date.day.to_s.rjust(2) + " " + + # 土曜日になったら新しいラインを作る + puts if date.saturday? +end + +puts "\n" + From 0de7d5ff8c40b5ea7bc92ac4ea2d9fe92500f38a Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Wed, 30 Aug 2023 15:11:55 +0900 Subject: [PATCH 2/3] bowling code --- 03.bowling/bowling.rb | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 03.bowling/bowling.rb diff --git a/03.bowling/bowling.rb b/03.bowling/bowling.rb new file mode 100644 index 0000000000..de14b8d4de --- /dev/null +++ b/03.bowling/bowling.rb @@ -0,0 +1,41 @@ +require 'rubocop' + +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 + +frames = [] +shots.each_slice(2) do |s| + frames << s +end + +point = 0 +frame_index = 0 + +frames.each_with_index do |frame, index| + if frame_index >= 9 + point += frame.sum + else + if frame[0] == 10 + point += 10 + frames[index + 1][0] + frames[index + 1][1] + frame_index += 1 + elsif frame.sum == 10 + point += 10 + frames[index + 1][0] + frame_index += 1 + else + point += frame.sum + frame_index += 1 + end + end +end + +puts point From b0b0b90e6bdc84363f873fe1ab2b0a9a9d237477 Mon Sep 17 00:00:00 2001 From: rani-i3 Date: Wed, 30 Aug 2023 17:25:30 +0900 Subject: [PATCH 3/3] fixed issues --- 03.bowling/bowling.rb | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/03.bowling/bowling.rb b/03.bowling/bowling.rb index de14b8d4de..b1b3215d15 100644 --- a/03.bowling/bowling.rb +++ b/03.bowling/bowling.rb @@ -1,4 +1,5 @@ require 'rubocop' +require 'debug' score = ARGV[0] scores = score.split(',') @@ -17,23 +18,33 @@ shots.each_slice(2) do |s| frames << s end - +p frames point = 0 -frame_index = 0 frames.each_with_index do |frame, index| - if frame_index >= 9 + # binding.break + if index >=9 point += frame.sum +# elsif frame_index == 11 + else if frame[0] == 10 - point += 10 + frames[index + 1][0] + frames[index + 1][1] - frame_index += 1 + if frames[index + 1][0] == 10 + if frames[index + 2][0] == 10 + point += 30 + else + point += 20 + frames[index + 2][0] + end + + else + point += 10 + frames[index + 1].sum + end + + elsif frame.sum == 10 point += 10 + frames[index + 1][0] - frame_index += 1 else point += frame.sum - frame_index += 1 end end end