Module: AocRb::PuzzleSolution

Extended by:
PuzzleSolution
Included in:
PuzzleSolution
Defined in:
lib/aoc_rb/puzzle_solution.rb

Instance Method Summary collapse

Instance Method Details

#submit(level, year, day, answer = nil, allow_waiting = true) ⇒ Object



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aoc_rb/puzzle_solution.rb', line 7

def submit(level, year, day, answer = nil, allow_waiting = true)
  if answer.nil?
    input    = PuzzleInput.load(year, day)
    puzzle   = PuzzleSource.create_puzzle(year, day, input)
    answer = level == 1 ? puzzle.part_1 : puzzle.part_2
  end

  aoc_api  = AocApi.new(ENV['AOC_COOKIE'])
  response = aoc_api.submit_answer(year, day, level, answer)
  # puts response.body

  wrong = /not the right answer/.match?(response.body)
  already_complete = /Did you already complete it/.match?(response.body)
  waiting_regex = /You have (\d*m* *\d+s) left to wait/
  waiting = waiting_regex.match?(response.body)

  doc = Nokogiri::HTML(response.body)
  articles = doc.css("article")
  puts articles[0].content

  puts "That's not the right answer" if wrong
  puts "You have already completed this challenge" if already_complete
  if waiting && allow_waiting
    delay = 1
    time = waiting_regex.match(response.body)[1]
    time_parts = time.split(" ")
    time_parts.each do |part|
      if part.match?(/m/)
        delay += part.match(/(\d+)m/)[1].to_i * 60
      elsif part.match?(/s/)
        delay += part.match(/(\d+)s/)[1].to_i
      end
    end

    puts "WAITING for #{delay} seconds ..."
    sleep delay
    return submit(level, year, day, answer, false)
  end

  !wrong && !already_complete && !waiting
end