diff --git a/lib/sg_strange_calendar.rb b/lib/sg_strange_calendar.rb index 5bfc5e5..41104c9 100644 --- a/lib/sg_strange_calendar.rb +++ b/lib/sg_strange_calendar.rb @@ -1,9 +1,74 @@ -class SgStrangeCalendar +require "date" +class SgStrangeCalendar + WEEKDAY_HEADER = "Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo" + def initialize(year, today = nil) - # write your code here + @year = year + @today = today + @calendar = create_calendar_hash(year) # {"Jan" => {1 => "Mo", 2=>"Tu", ... }, "Feb"=>{1=>"Th", ... }, ...} end def generate(vertical: false) - # write your code here + result = [@year.to_s + " " + WEEKDAY_HEADER] + @calendar.each do |month, date| + result.push(create_month_line(month, date)) + end + adjust_space(result.join("\n")) + end + + def create_calendar_hash(year) + result = {} + + Date.new(year, 1, 1).step(Date.new(year, 12, 31)).each do |date| + month = date.strftime("%b") # ex: Jan + weekday = date.strftime("%a")[0, 2] # ex: Mo + + result[month] ||= {} + result[month][date.day] = weekday + end + + result + end + + def create_month_line(month, date) + result = [month.ljust(4)] + target_date = 1 + + WEEKDAY_HEADER.split(" ").each do |w| + target_weekday = date[target_date] + if target_weekday.nil? + break + end + + if w == target_weekday + if isToday(month, target_date) + result.push("[" + target_date.to_s + "]") + else + result.push(target_date.to_s.rjust(2)) + end + target_date += 1 + else + result.push(" ") + end + end + + result.join(" ") + end + + def isToday(targe_month, target_date) + !@today.nil? && @today.strftime("%Y") == @year.to_s && @today.strftime("%b") == targe_month && @today.strftime("%d") == target_date.to_s.rjust(2, "0") end end + +def adjust_space(target) + if @today.nil? + return target + end + + @today.strftime("%d").to_i < 10 ? target.gsub("] ", "]") : target.gsub(" [", "[").gsub("] ", "]") + +end + +today = Date.new(2024, 12, 31) +c = SgStrangeCalendar.new(2024, today) +c.generate diff --git a/test/sg_strange_calendar_test.rb b/test/sg_strange_calendar_test.rb index 472d1d2..cb2e8ba 100644 --- a/test/sg_strange_calendar_test.rb +++ b/test/sg_strange_calendar_test.rb @@ -44,7 +44,7 @@ def test_level_1_for_2025 end def test_level_2_for_2024_01_01 - skip "レベル2にチャレンジする人はこの行を削除してください" + # skip "レベル2にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Jan [1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 @@ -66,7 +66,6 @@ def test_level_2_for_2024_01_01 end def test_level_2_for_2024_12_09 - skip "レベル2にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2024 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Jan 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 @@ -88,7 +87,6 @@ def test_level_2_for_2024_12_09 end def test_level_2_for_2025_03_31 - skip "レベル2にチャレンジする人はこの行を削除してください" expected = <<~TXT.chomp 2025 Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Jan 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 @@ -110,7 +108,7 @@ def test_level_2_for_2025_03_31 end def test_level_2_all - skip "レベル2にチャレンジする人はこの行を削除してください" + # skip "レベル2にチャレンジする人はこの行を削除してください" file_path = File.expand_path('level2.txt', File.dirname(__FILE__)) calendars = File.read(file_path).lines.each_slice(13).map(&:join).map(&:chomp) from_date = Date.new(2025, 1, 1)