-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.rb
30 lines (28 loc) · 960 Bytes
/
input.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
# To simplify starting a new day on the Advent of Code challenge.
#
# Prerequisite: the presence of the ./.session file containing the "session=19827364918273..."
# value for the cookie.
#
# Returns the cached input or retrieves and caches the indicated day's input.
#
# Note that some days may not have an input file if the input was specified in the description.
require 'date'
require 'uri'
require 'net/http'
module Input
extend self
def for_day(day, year=Date.today.year)
cache_file = 'day%02d_input.txt'%[day]
if File.exist? cache_file
File.read(cache_file)
else
uri = URI('http://adventofcode.com/%d/day/%s/input'%[year, day.to_s])
req = Net::HTTP::Get.new(uri)
req['Cookie'] = File.read('./.session') if File.exist?('./.session')
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
res.body.tap {|contents| File.write(cache_file, contents) }
end
end
end