-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoc.rb
38 lines (30 loc) · 926 Bytes
/
Aoc.rb
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
32
33
34
35
36
37
38
require 'net/http'
require 'tmpdir'
module Aoc
def get_input(year:, day:, ignore_cache: false)
if (not ignore_cache) && cache_exists?(year, day)
return read_cache(year, day)
end
aoc_session = File.read('AOC_SESSION.txt').strip
uri = URI("https://adventofcode.com/#{year}/day/#{day}/input")
req = Net::HTTP::Get.new(uri)
req['Cookie'] = "session=#{aoc_session}"
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end
write_cache(year, day, response.body)
read_cache(year, day)
end
def cache_exists?(year, day)
File.exist?(cache_path(year, day))
end
def read_cache(year, day)
File.readlines(cache_path(year, day)).map(&:strip)
end
def write_cache(year, day, input)
File.write(cache_path(year, day), input)
end
def cache_path(year, day)
"#{Dir.tmpdir}/aoc_input_#{year}_#{day}.txt"
end
end