Emacs Advent of Code helper

Grab your input like a pro

One step in solving the Advent of Code challenges is to grab the puzzle input. This step is easily automated within Emacs.

The following function reads the year and day from the input and then inserts the contents of the url into the current buffer. This works quite nicely.

Updated: it now uses the current date as input unless it is prefixed (C-u). When prefixed you can give it a year and a date and it will pull in that specific input.

Another update: a temp buffer will pop up that will display the text of the puzzle.

(defun aw/aoc-get-puzzle-and-input (prefix)
  "Retrieve the input for an advent of code puzzle. Use a prefix
to retrieve for a specific year or day. The input is inserted into the current
buffer, a new buffer will be made with the description of the puzzle."
  (interactive "p")
  (let* ((year  (cond ((= prefix 1)
                       (string-to-number (format-time-string "%Y")))
                      ((> prefix 1)
                       (read-number "Year: "))))
         (day (cond ((= prefix 1)
                     (string-to-number (format-time-string "%d")))
                    ((> prefix 1)
                     (read-number "Day: "))))
         (creds (auth-source-search :host "adventofcode.com"))
         (secret (plist-get (car creds) :secret))
         (decoded (funcall secret))
         (url-request-extra-headers `(("Cookie" . ,(concat "session=" decoded)))))
    (url-insert-file-contents (format "https://adventofcode.com/%d/day/%d/input" year day))
    (with-output-to-temp-buffer "aoc-puzzle"
        (pop-to-buffer "aoc-puzzle")
        (url-insert-file-contents (format "https://adventofcode.com/%d/day/%d" year day))
        (shr-render-region (point-min) (point-max)))
    ))

In order for it to work it is required to grab the session cookie from the website, just hist F12, scroll to storage and copy the hash you see there.

storage.png

Add this hash to your .authinfo file.

machine adventofcode.com password 1234YOURHASHHERE

Emacs will now happily grab the input for you and insert it into the current buffer and show you the challenge ahead.

Go back to the homepage.