#!/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 { File.directory?(_1) && !_1.match?(/^\./) } browser.goto("https://adventofcode.com/") cookies = JSON.parse(File.read("tmp/cookies.json")) cookies.each { browser.cookies.add(_1["name"], _1["value"], domain: _1["domain"]) } pre_element = browser.element(tag_name: "pre", class: "calendar") doc = Nokogiri::HTML(pre_element.html) links = doc.css("a").map { _1["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