-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbb.edn
More file actions
81 lines (75 loc) · 3.5 KB
/
Copy pathbb.edn
File metadata and controls
81 lines (75 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{:paths ["input" "Clojure"]
:deps {org.clojure/math.combinatorics {:mvn/version "0.1.6"}
org.clj-commons/digest {:mvn/version "1.4.100"}}
:tasks
{:requires ([babashka.curl :as curl]
[babashka.fs :as fs]
[clojure.string :refer [join split-lines]])
:init
(do
(def input-dir "input")
(def solutions-dir "Clojure")
(def template-path (format "%s/.template.txt" solutions-dir))
(def day-path (partial format "%s/%s.clj" solutions-dir))
(defn exit! [& msgs] (apply println msgs) (System/exit 1))
(def config
(try
(let [re (re-pattern "AOC_SESSION=(\\w+)|YEAR=(\\d+)"), [[_ token _] [_ _ year]] (re-seq re (slurp ".env"))]
{:token token :year year})
(catch java.io.FileNotFoundException e (exit! (str e)))))
(def yd->url (partial format "https://adventofcode.com/%s/day/%s/input"))
(def today (.format (java.text.SimpleDateFormat. "d") (new java.util.Date)))
(def current-year (.format (java.text.SimpleDateFormat. "y") (new java.util.Date)))
(def input-path (partial format "%s/%s" input-dir))
(defn day-fmt [d] (format "day%02d" (parse-long d)))
(defn fetch-input [url token] (curl/get url {:raw-args ["--cookie" (format "session=%s" token)] :throw false}))
(defn mkdir [d] (when-not (fs/exists? d) (println "Creating" d) (fs/create-dir d)))
(defn d-and-yr [d yr] [(or d today) (or yr (:year config) current-year)])
;; sanity checks
(when-not (fs/exists? template-path) (exit! template-path "does not exist, please create it!"))
(doseq [d [input-dir solutions-dir]] (when-not (fs/exists? d) (fs/create-dir d)))
)
template
{:doc " create template for solution [day and year default to current]"
:task
(let [[d _] *command-line-args*, [d _] (d-and-yr d nil)
day (day-fmt d), fp (day-path day)
tpl (format (slurp template-path) day)]
(when (fs/exists? fp) (exit! fp "already exists!"))
(println "Creating template for day" d)
(spit fp tpl))
}
fetch
{:doc " get input for given day [requires session token stored in `.env` file ; args default to today]"
:task
(let [[d yr] *command-line-args*, [d yr] (d-and-yr d yr)
url (yd->url yr d), day (day-fmt d), path (input-path day)
{:keys [status body]} (fetch-input url (:token config))]
(when-not (= status 200) (exit! status body))
(spit path body)
(let [msg (format "AoC %s day %s input successfully fetched!" yr d)
amount 30, lines (split-lines (slurp path))]
(println msg)
(if (>= (count lines) amount)
(println (format "Showing first %d lines:" amount))
(println "Fetched input:"))
(println (apply str (take (count msg) (repeat "-"))))
(println (join "\n" (take 30 lines)))
(let [rst (drop amount lines)]
(when-not (empty? rst) (prn (format "[... %d lines skipped ...]" (count rst)))))))}
boot
{:doc " fetch input data and create template for given date"
:depends [fetch template]}
solve
{:doc " run solution for given day [day defaults to today]"
:task
(let [d (or (first *command-line-args*) today), day (day-fmt d)
fnc (symbol day "-main")]
(try
;; a propos `requiring-resolve` see also `exec` https://book.babashka.org/#cli:exec
(let [solutions (time ((requiring-resolve fnc) day))]
(doseq [[k v] solutions] (printf "%s %s%n" k v)))
(catch java.lang.Exception e
(println "Could not run solution for day" d)
(println "Error:" (str e)))))}
}}