1 (ns adventofcode2019.lib
2 [:require [clojure.string :as str]
4 [clojure.java.io :as io]
5 [clojure.java.shell :refer [sh]]
6 [clojure.data.priority-map :refer [priority-map-by]]])
8 (defn get-list-from-file
10 (str/split-lines (str/trim (slurp file-name))))
11 ([file-name split-regex]
12 (str/split (str/trim (slurp file-name)) split-regex)))
15 (let [n-val (edn/read-string n)]
18 (throw (Exception. "Not a number!")))))
20 (defmacro input-file []
21 (let [bottom-ns (last (str/split (str *ns*) #"\."))]
22 (str "resources/" bottom-ns)))
24 (defn manhattan-distance [[ax ay] [bx by]]
25 (+ (Math/abs (- ax bx))
26 (Math/abs (- ay by))))
29 "succ :: state -> [state]
30 cost :: cost -> state -> cost
33 des? :: state -> bool"
34 [succ cost heur init des?]
35 (let [update-openl (fn [ol sn pt cs]
37 (assoc o p [(cost cs p) (heur p)]))
38 ol (remove sn (succ pt))))
39 openl (priority-map-by (fn [[ag ah] [bg bh]]
40 (let [cmp (compare (+ ag ah) (+ bg bh))
42 cmp-h (compare ah bh)]
48 (loop [openl (assoc openl init [0 (heur init)])
50 (let [[point [dist _]] (peek openl)]
52 (empty? openl) [nil ##Inf]
53 (des? point) [point dist]
54 :else (let [openl (update-openl (pop openl) seen point dist)
55 seen (apply conj seen (keys openl))]
56 (recur openl seen)))))))
59 (zipmap (keys m) (map f (vals m))))
62 #(println (str "Part 1: " %)))
64 #(println (str "Part 2: " %)))
66 ;; FIXME: this is still broken but i give up for now
67 ; (defn --input-file [for-ns]
68 ; (let [bottom-ns (last (str/split for-ns #"\."))
69 ; input-url "https://adventofcode.com/2019/day/%d/input"
70 ; day-url (->> bottom-ns
75 ; token (str/trim (slurp (io/resource "token")))
76 ; res-dir (-> (io/resource "token")
78 ; (str/replace "token" bottom-ns))
79 ; cmd ["curl" "-s" "-b"
80 ; (format "\"session=%s\"" token)
81 ; day-url ">" res-dir]]
82 ; (if-let [input (io/resource bottom-ns)]
84 ; (do (apply sh cmd) nil))))
86 ; (defmacro input-file []
87 ; (--input-file (str *ns*)))