-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbb.edn
81 lines (77 loc) · 2.98 KB
/
bb.edn
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 ["."]
:tasks
{:requires ([babashka.curl :as curl]
[babashka.fs :as fs]
[clojure.string :refer [join split-lines]])
:init
(do
(defn exit! [& msgs] (apply println msgs) (System/exit 1))
(def token
(try (slurp ".token")
(catch java.io.FileNotFoundException e (exit! (str e)))))
(def yd->url (partial format "https://adventofcode.com/%s/day/%s/input"))
(def dirname (partial format "day%02d"))
(def fname (partial format "%s/input.txt"))
(defn d-or-today [d]
(or d (.format (java.text.SimpleDateFormat. "d") (new java.util.Date))))
(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]
[(d-or-today d) (or yr "2022")])
)
template
{:doc " create template for solution [day and year default to current]"
:task
(let [[d yr] *command-line-args*
[d* yr*] (d-and-yr d yr)
dint (parse-long d*)
dir (dirname dint)
fp (str dir "/" "solution.clj")
tpl (format (slurp ".template.txt") dint)]
(when (fs/exists? fp) (exit! fp "already exists!"))
(mkdir dir)
(println "Creating template for day" d*)
(spit fp tpl))}
fetch
{:doc " get input for given day [requires session token stored in `.token` file ; args default to today]"
:task
(let [[d yr] *command-line-args*
[d* yr*] (d-and-yr d yr)
url (yd->url yr* d*)
dir (dirname (parse-long d*))
path (fname dir)
{:keys [status body]} (fetch-input url token)
ok? (= status 200)]
(when-not ok? (exit! status body))
(mkdir dir)
(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)
(println (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 (Integer. (d-or-today (first *command-line-args*)))
day (format "day%02d" d)
fnc (symbol (str day ".solution") "-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)))))}}}