add input manager

This commit is contained in:
Adam Jeniski 2024-12-01 12:30:52 -05:00
parent 14ce95a390
commit 3b2f1917ac
3 changed files with 33 additions and 28 deletions

View File

@ -1,25 +1,4 @@
(ns core
(:require
[babashka.fs :as fs]
[babashka.http-client :as http]
[clojure.string :as str]))
(def session (first (fs/read-all-lines "input/.session")))
(defn input-file-loc [day]
(str "input/" day ".txt"))
(defn download-input [day]
(->> {:headers {:cookie (str "session=" session)}}
(http/get (str "https://adventofcode.com/2024/day/" day "/input"))
:body
(str/split-lines)
(fs/write-lines (input-file-loc day))))
(defn get-input [day]
(try
(fs/read-all-lines (input-file-loc day))
(catch java.nio.file.NoSuchFileException _e
(download-input day)
(get-input day))))
(ns core)
(defn compose [& fs]
(apply comp (reverse fs)))

View File

@ -1,11 +1,12 @@
(ns day01
(:require
[core :refer [get-input]]))
[input-manager :refer [get-input]]
[core :refer :all]))
(def input (->> (get-input 1)
(map #(re-seq #"(\d+)\s+(\d+)" %))
(map (comp rest first))
(map (partial mapv parse-long))
(map (compose #(re-seq #"(\d+)\s+(\d+)" %)
first rest ; only get match groups
#(mapv parse-long %)))
(into {})
((juxt keys vals))))

View File

@ -0,0 +1,25 @@
(ns input-manager
(:require
[babashka.fs :as fs]
[babashka.http-client :as http]
[clojure.string :as str]))
(def session (first (fs/read-all-lines "input/.session")))
(defn input-file-loc [day]
(str "input/" day ".txt"))
(defn download-input [day]
(->> {:headers {:cookie (str "session=" session)}}
(http/get (str "https://adventofcode.com/2024/day/" day "/input"))
:body
(str/split-lines)
(fs/write-lines (input-file-loc day))))
(defn get-input [day]
(try
(fs/read-all-lines (input-file-loc day))
(catch java.nio.file.NoSuchFileException _e
(download-input day)
(get-input day))))