Skip to content

Commit 89549b0

Browse files
committed
Improved scaffolding
1 parent ea011b3 commit 89549b0

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Solutions for the 2016 edition of the Advent of Code - http://adventofcode.com/2
44

55
Back again for another year, the little [Advent of Code] site that has a new pair of problems to solve each new day (i.e., midnight Eastern time) and this repository holds my solutions using [ruby](http://ruby-lang.org)
66

7+
There are a few files that help with the mechanics of the challenge itself. The value for the `session` cookie from teh site is stored in the `./.session` file. The `day_prep.rb` retrieves the challenge description and builds the initial solution file using the `day_template.rb`. The input for the day's challenge is retrieved (and cached!) using code from the module defined in `input.rb`.
8+
79
My solutions for the original 2015 installment of the Advent of Code is in the repo: [rab/adventofcode2015](https://github.com/rab/adventofcode2015)
810

911
My Personal [Leaderboard] stats:

day_prep.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# To simplify starting a new day on the Advent of Code challenge.
2+
#
3+
# Usage: ruby day_prep.rb [day]
4+
#
5+
# Prerequisite: the presence of the ./.session file containing the "session=19827364918273..."
6+
# value for the cookie.
7+
#
8+
# When: Runs anytime during the day (Eastern time) to get the challenge from the current day or
9+
# from a single past date by supplying the day number as an argument.
10+
#
11+
# The "Part Two" will typically have to be retrieved manually.
12+
#
13+
require 'date'
14+
require 'uri'
15+
require 'net/http'
16+
require 'nokogiri'
17+
18+
day = ARGV[0].to_i.nonzero? || Date.today.day
19+
year = Date.today.year
20+
filename = 'day%02d.rb'%[day]
21+
unless File.exist? filename
22+
if File.exist?(html = filename.sub(/\.rb\z/,'.html'))
23+
doc = Nokogiri::HTML File.read(html)
24+
else
25+
uri = URI('http://adventofcode.com/%d/day/%d'%[year, day])
26+
req = Net::HTTP::Get.new(uri)
27+
req['Cookie'] = File.read('./.session') if File.exist?('./.session')
28+
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
29+
http.request(req)
30+
}
31+
doc = Nokogiri::HTML res.body.tap {|contents| File.write(html, contents) }
32+
end
33+
34+
File.open(filename, 'w') do |f|
35+
doc.css('article.day-desc').children.each do |node|
36+
f.puts node.text.gsub(/^/,'# ')
37+
end
38+
f.puts
39+
f.write File.read('day_template.rb')
40+
end
41+
end

input.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# To simplify starting a new day on the Advent of Code challenge.
2+
#
3+
# Prerequisite: the presence of the ./.session file containing the "session=19827364918273..."
4+
# value for the cookie.
5+
#
6+
# Returns the cached input or retrieves and caches the indicated day's input.
7+
#
8+
# Note that some days may not have an input file if the input was specified in the description.
9+
110
require 'uri'
211
require 'net/http'
312

0 commit comments

Comments
 (0)