Skip to content

Instantly share code, notes, and snippets.

@PEZ
Last active December 3, 2022 23:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PEZ/bcd23b9bc099b8b21a41c8c9de78a0f4 to your computer and use it in GitHub Desktop.
Save PEZ/bcd23b9bc099b8b21a41c8c9de78a0f4 to your computer and use it in GitHub Desktop.
Utility for loading the Advent of Code 2020 input into the Clojure REPL on demand
(ns pez.aoc2020.util
(:require [clj-http.client :as client]))
(defn fetch-input [day]
(try
(let [cookie (slurp ".aoc-session")]
(:body (client/get
(str "https://adventofcode.com/2020/day/" day "/input")
{:cookies {"session" {:value cookie}}})))
(catch Exception e
(println "Ho, ho, ho! Did you forget to populate `.aoc-session` with your AOC session cookie?")
(throw e))))
@PEZ
Copy link
Author

PEZ commented Dec 2, 2020

This is a utility for fetching Advent of Code 2020 input data.

How to use:

In preparation. (The cookie lives for only 10 years, so you need to redo this in a decade):

  1. Be logged in at adventofcode.com/2020 and open the problem page
  2. From Developer tools of the browser (Network tab), Copy the session cookie from the page resource (named just 2020 for index page).
  3. Store this in the file .aoc-session at the root of your project.

When trying to collect a star or two:

  1. Evaluate (util/fetch-input <day>) (probably into a def)

It looked like this in my Rich comment block for Day 2 (don't worry, no spoilers).

(comment
  (def input
    "1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc")
  (def input (util/fetch-input 2))
  (->> input
       (parse)
       (filter checks2?)
       (map :pwd)
       (count)))

I made do with the sample input until I thought my solution worked, then redefined input by fetching the problem input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment