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" + diff --git a/03.bowling/bowling.rb b/03.bowling/bowling.rb new file mode 100644 index 0000000000..b1b3215d15 --- /dev/null +++ b/03.bowling/bowling.rb @@ -0,0 +1,52 @@ +require 'rubocop' +require 'debug' + +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 +p frames +point = 0 + +frames.each_with_index do |frame, index| + # binding.break + if index >=9 + point += frame.sum +# elsif frame_index == 11 + + else + if frame[0] == 10 + 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] + else + point += frame.sum + end + end +end + +puts point