mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 09:33:19 -09:00
add input manager
This commit is contained in:
parent
14ce95a390
commit
3b2f1917ac
@ -1,25 +1,4 @@
|
|||||||
(ns core
|
(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))))
|
|
||||||
|
|
||||||
|
(defn compose [& fs]
|
||||||
|
(apply comp (reverse fs)))
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
(ns day01
|
(ns day01
|
||||||
(:require
|
(:require
|
||||||
[core :refer [get-input]]))
|
[input-manager :refer [get-input]]
|
||||||
|
[core :refer :all]))
|
||||||
|
|
||||||
(def input (->> (get-input 1)
|
(def input (->> (get-input 1)
|
||||||
(map #(re-seq #"(\d+)\s+(\d+)" %))
|
(map (compose #(re-seq #"(\d+)\s+(\d+)" %)
|
||||||
(map (comp rest first))
|
first rest ; only get match groups
|
||||||
(map (partial mapv parse-long))
|
#(mapv parse-long %)))
|
||||||
(into {})
|
(into {})
|
||||||
((juxt keys vals))))
|
((juxt keys vals))))
|
||||||
|
|
||||||
|
25
2024/src/input_manager.clj
Normal file
25
2024/src/input_manager.clj
Normal 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))))
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user