-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2019-11-space-police.clj
More file actions
38 lines (32 loc) · 1.13 KB
/
Copy path2019-11-space-police.clj
File metadata and controls
38 lines (32 loc) · 1.13 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
(require '[clojure.core.async :as async]
'[intcode :refer [intcode]])
(def turn
[{[0 -1] [-1 0] [-1 0] [0 1] [0 1] [1 0] [1 0] [0 -1]}
{[0 -1] [1 0] [1 0] [0 1] [0 1] [-1 0] [-1 0] [0 -1]}])
(defn move [[ax ay] [bx by]]
[(+ ax bx) (+ ay by)])
(def color [\░ \█])
(defn length [f xs]
(range (inc (transduce (map (comp f key)) max 0 xs))))
(defn run [xs init]
(let [[src dst out] (intcode xs)]
(async/<!!
(async/go-loop [pos [0 0] dir [0 -1] rs {[0 0] init}]
(async/alt!
[[src (get rs pos 0)]]
(let [col (async/<! dst)
rot (async/<! dst)
dir ((turn rot) dir)]
(recur (move pos dir) dir (assoc rs pos col)))
out rs)))))
(let [in (first (line-seq (java.io.BufferedReader. *in*)))
xf (comp (map parse-long) (map-indexed vector))
xs (into {} xf (re-seq #"-?\d+" in))]
(println "Part A:" (count (run xs 0)))
(println "Part B:")
(let [rs (run xs 1)]
(doseq [y (length (fn [[_ y]] y) rs)]
(println
(transduce
(comp (map (fn [x] (get rs [x y] 0))) (map color)) str
(length (fn [[x _]] x) rs))))))