-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2016-13-maze-twisty-cubicles.clj
More file actions
40 lines (35 loc) · 1.39 KB
/
Copy path2016-13-maze-twisty-cubicles.clj
File metadata and controls
40 lines (35 loc) · 1.39 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
(def queue clojure.lang.PersistentQueue/EMPTY)
(def dirs [[0 -1] [1 0] [0 1] [-1 0]])
(defn add [[ax ay] [bx by]]
[(+ ax bx) (+ ay by)])
(defn space? [[x y] z]
(and (>= x 0) (>= y 0)
(->> (+ (* x x) (* 3 x) (* 2 x y) (* y y) y z)
(Long/bitCount)
(even?))))
(defn minimum-steps [xf src dst]
(loop [queue (conj queue [src]) visit (hash-set)]
(let [path (peek queue)
node (peek path)]
(cond (= node dst) (dec (count path))
(visit node) (recur (pop queue) visit)
:else (recur (into (pop queue) (xf path) dirs) (conj visit node))))))
(defn unique-points [xf src lim]
(loop [queue (conj queue [src]) visit (hash-set)]
(if-let [path (peek queue)]
(let [node (peek path)]
(cond (= (dec (count path)) lim)
(recur (pop queue) (conj visit node))
(visit node)
(recur (pop queue) visit)
:else
(recur (into (pop queue) (xf path) dirs) (conj visit node))))
(count visit))))
(let [in (line-seq (java.io.BufferedReader. *in*))
xn (parse-long (first in))
xf (fn [path]
(comp (map (fn [off] (add (peek path) off)))
(filter (fn [pos] (space? pos xn)))
(map (fn [pos] (conj path pos)))))]
(println "Part A:" (minimum-steps xf [1 1] [31 39]))
(println "Part B:" (unique-points xf [1 1] 50)))