module AocRb::PuzzleInput

Public Instance Methods

create_required_directories(year, day) click to toggle source
# File lib/aoc_rb/puzzle_input.rb, line 12
def create_required_directories(year, day)
  padded_day = day.to_s.rjust(2, "0")
  year_directory = File.join("challenges", year.to_s, padded_day)
  FileUtils.mkdir_p(year_directory) unless Dir.exist?(year_directory)
end
download(year, day) click to toggle source
# File lib/aoc_rb/puzzle_input.rb, line 23
def download(year, day)
  aoc_api = AocRb::AocApi.new(ENV['AOC_COOKIE'])
  content = aoc_api.puzzle_input(year, day)
  save_puzzle(year, day, content)
end
load(year, day) click to toggle source
# File lib/aoc_rb/puzzle_input.rb, line 6
def load(year, day)
  file_path = puzzle_path(year, day)
  download(year, day) unless File.exist? file_path
  File.read(file_path)
end
puzzle_path(year, day) click to toggle source
# File lib/aoc_rb/puzzle_input.rb, line 18
def puzzle_path(year, day)
  padded_day = day.to_s.rjust(2, "0")
  File.join("challenges", year.to_s, padded_day, "input.txt")
end
save_puzzle(year, day, content) click to toggle source
# File lib/aoc_rb/puzzle_input.rb, line 29
def save_puzzle(year, day, content)
  protect_against_early_download(content)
  create_required_directories year, day
  skip_if_exists(puzzle_path(year, day)) do
    File.open(puzzle_path(year, day), "w") { |f| f.write content }
  end
end
skip_if_exists(file) { || ... } click to toggle source
# File lib/aoc_rb/puzzle_input.rb, line 37
def skip_if_exists(file)
  unless File.exist? file
    yield
  else
    puts "#{file} already exists, skipping"
  end
end

Private Instance Methods

protect_against_early_download(content) click to toggle source
# File lib/aoc_rb/puzzle_input.rb, line 47
def protect_against_early_download(content)
  if /the link will be enabled on the calendar the instant this puzzle becomes available/.match?(content)
    puts "ERROR: This resource is not available for download yet"
    exit 0
  end
end