1
0
Fork 0

Add problem downloader

This commit is contained in:
Andrew Tomaka 2024-12-10 19:45:42 -05:00
parent 01ad55fcbd
commit faff51b9d3
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
6 changed files with 93 additions and 1 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
tmp/

View file

@ -1,3 +1,5 @@
source "https://rubygems.org"
gem "nokogiri"
gem "rspec"
gem "watir"

View file

@ -1,7 +1,18 @@
GEM
remote: https://rubygems.org/
specs:
base64 (0.2.0)
diff-lcs (1.5.1)
logger (1.6.2)
mini_portile2 (2.8.8)
nokogiri (1.17.1)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.17.1-arm64-darwin)
racc (~> 1.4)
racc (1.8.1)
regexp_parser (2.9.3)
rexml (3.3.9)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
@ -15,13 +26,26 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.2)
rubyzip (2.3.2)
selenium-webdriver (4.27.0)
base64 (~> 0.2)
logger (~> 1.4)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
watir (7.3.0)
regexp_parser (>= 1.2, < 3)
selenium-webdriver (~> 4.2)
websocket (1.2.11)
PLATFORMS
arm64-darwin-24
ruby
DEPENDENCIES
nokogiri
rspec
watir
BUNDLED WITH
2.5.23

65
download-problems Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env ruby
require "fileutils"
require "html2markdown"
require "nokogiri"
require "watir"
require "debug"
browser = Watir::Browser.new(:chrome, headless: true)
def login(browser)
browser.goto("https://adventofcode.com/auth/github")
browser.text_field(name: "login").set(ENV.fetch("GITHUB_USERNAME"))
browser.text_field(name: "password").set(ENV.fetch("GITHUB_PASSWORD"))
browser.button(name: "commit").click
code = browser.text.scan(/Enter the digits shown below to verify your identity.\n(\d+)/)
if browser.url.include?("two-factor")
puts "Complete 2FA on phone. Code: #{code}"
Watir::Wait.until(timeout: 120) do
!browser.url.include?("two-factor")
end
end
if browser.button(name: "authorize").exists?
browser.button(name: "authorize").click
end
final_url = browser.url
browser.goto(final_url)
session_cookies = browser.cookies.to_a
File.write("tmp/cookies.json", session_cookies.to_json)
browser.close
end
local = Dir
.entries(".")
.select { |entry| File.directory?(entry) && !entry.match?(/^\./) }
browser.goto("https://adventofcode.com/")
cookies = JSON.parse(File.read("tmp/cookies.json"))
cookies.each { |cookie| browser.cookies.add(cookie["name"], cookie["value"], domain: cookie["domain"]) }
pre_element = browser.element(tag_name: "pre", class: "calendar")
doc = Nokogiri::HTML(pre_element.html)
links = doc.css("a").map { |link| link["href"] }
available = links.map { _1.scan(/day\/(\d+)/) }.flatten.map { "%02d" % _1 }
missing = available - local
if missing.any?
missing.each do |problem|
Dir.mkdir(problem)
browser.goto("https://adventofcode.com/2024/day/#{problem}/input")
File.write("#{problem}/input", browser.text)
FileUtils.copy("./template.rb", "./#{problem}/main.rb")
end
else
puts "All problems are currently local"
end

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby
problem = :fake
#input = File.readlines("#{problem}.input", chomp: true)
#input = File.readlines("input", chomp: true)
input = DATA.read
result = :solution

0
tmp/.gitinclude Normal file
View file