Module: Arb::Util

Defined in:
lib/arb/util.rb

Class Method Summary collapse

Class Method Details

.max_day(year:) ⇒ Object



39
40
41
# File 'lib/arb/util.rb', line 39

def self.max_day(year:)
  (year.to_i >= 2025) ? 12 : 25 # only 12 days starting in 2025
end

.validate_year_and_day(year:, day:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/arb/util.rb', line 43

def self.validate_year_and_day(year:, day:)
  if year < years_and_max_days.keys.first
    raise InputError, "Year must be at least 2015, when Advent of Code started."
  end
  if year > years_and_max_days.keys.last
    raise InputError, "Advent of Code #{year} is still in the future."
  end
  if Date.new(year, 12, day) > Date.today
    raise InputError, "Day must be <= today."
  end
  if !day.between?(1, years_and_max_days[year])
    raise InputError, "For #{year}, day must be between 1 and #{years_and_max_days[year]}."
  end
end

.years_and_max_daysObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/arb/util.rb', line 18

def self.years_and_max_days
  return @years_and_max_days if @years_and_max_days

  max_year =
    if Date.today.month == 12
      Date.today.year
    else
      Date.today.year - 1
    end

  @years_and_max_days = (2015..max_year).map { |year|
    max_day = max_day(year:)

    if year == Date.today.year
      [year, Date.today.day.clamp(0..max_day)]
    else
      [year, max_day]
    end
  }.to_h
end