Skip to content

Instantly share code, notes, and snippets.

Created December 14, 2022 14:04
What would you like to do?
Get Advent of Code input in babashka.
#!/bin/env bb
(require '[babashka.fs :as fs])
(require '[babashka.curl :as curl])
(defn get-day-component []
(let [components (fs/components (fs/cwd))]
(-> (filter #(str/starts-with? % "day") components)
(defn get-day-number [day-name]
(-> (str/replace day-name "day" "")
(defn build-url [day]
(format "" day))
(defn get-day-url []
(let [day-name (get-day-component)]
(when day-name
(build-url (get-day-number day-name)))))
(def cookie-value (edn/read ( (io/reader (fs/file (fs/parent *file*) "aoc-cookie")))))
(defn fetch-input []
(let [url (get-day-url)]
(when url
(curl/get url {:raw-args ["-b" (format "session=%s" cookie-value)]}))))
(defn output-file []
(loop [comp (fs/components (fs/cwd))]
(if (str/starts-with? (last comp) "day")
(str (fs/file "/" (apply fs/file comp) "input.txt"))
(recur (butlast comp)))))
(let [out (output-file)]
(and (not (fs/exists? out))
(spit (io/writer out) (:body (fetch-input)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment