Module: AocRb::PuzzleInput

Extended by:
PuzzleInput
Included in:
PuzzleInput
Defined in:
lib/aoc_rb/puzzle_input.rb

Instance Method Summary collapse

Instance Method Details

#create_required_directories(year, day) ⇒ Object



13
14
15
16
17
# File 'lib/aoc_rb/puzzle_input.rb', line 13

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) ⇒ Object



24
25
26
27
28
# File 'lib/aoc_rb/puzzle_input.rb', line 24

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) ⇒ Object



7
8
9
10
11
# File 'lib/aoc_rb/puzzle_input.rb', line 7

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) ⇒ Object



19
20
21
22
# File 'lib/aoc_rb/puzzle_input.rb', line 19

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) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/aoc_rb/puzzle_input.rb', line 30

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) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/aoc_rb/puzzle_input.rb', line 38

def skip_if_exists(file)
  unless File.exist? file
    yield
  else
    puts "#{file} already exists, skipping"
  end
end