SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn up [p]
  2.   (update p 1 inc))
  3.  
  4. (defn down [p]
  5.   (update p 1 dec))
  6.  
  7. (defn left [p]
  8.   (update p 0 dec))
  9.  
  10. (defn right [p]
  11.   (update p 0 inc))
  12.  
  13. (defn distance [p]
  14.   (apply + (map #(Math/abs %) p)))
  15.  
  16. (def dir->fn
  17.   {\U up
  18.    \D down
  19.    \L left
  20.    \R right})
  21.  
  22. (defn movement-points [current-point movement]
  23.   (let [dir (first movement)
  24.         cnt (Integer/valueOf (apply str (rest movement)))
  25.         next-point (dir->fn dir)]
  26.     (take cnt (iterate next-point (next-point current-point)))))
  27.  
  28. (defn parse-path [string-path]
  29.   (clojure.string/split string-path #","))
  30.  
  31. (defn path-points [string-path]
  32.   (let [path (parse-path string-path)]
  33.     (loop [path path
  34.            points [[0 0]]]
  35.       (if path
  36.         (recur (next path)
  37.                (concat points
  38.                        (movement-points (last points) (first path))))
  39.         points))))
  40.  
  41. (defn intersections [sp1 sp2]
  42.   (-> (clojure.set/intersection
  43.        (set (path-points sp1))
  44.        (set (path-points sp2)))
  45.       (disj [0 0])))
  46.  
  47. (let [[sp1 sp2] (clojure.string/split-lines (slurp "/tmp/input"))]
  48.     (->> (intersections sp1 sp2)
  49.          (map distance)
  50.          sort
  51.          first)) ;; => 870
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top