Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn up [p]
- (update p 1 inc))
- (defn down [p]
- (update p 1 dec))
- (defn left [p]
- (update p 0 dec))
- (defn right [p]
- (update p 0 inc))
- (defn distance [p]
- (apply + (map #(Math/abs %) p)))
- (def dir->fn
- {\U up
- \D down
- \L left
- \R right})
- (defn movement-points [current-point movement]
- (let [dir (first movement)
- cnt (Integer/valueOf (apply str (rest movement)))
- next-point (dir->fn dir)]
- (take cnt (iterate next-point (next-point current-point)))))
- (defn parse-path [string-path]
- (clojure.string/split string-path #","))
- (defn path-points [string-path]
- (let [path (parse-path string-path)]
- (loop [path path
- points [[0 0]]]
- (if path
- (recur (next path)
- (concat points
- (movement-points (last points) (first path))))
- points))))
- (defn intersections [sp1 sp2]
- (-> (clojure.set/intersection
- (set (path-points sp1))
- (set (path-points sp2)))
- (disj [0 0])))
- (let [[sp1 sp2] (clojure.string/split-lines (slurp "/tmp/input"))]
- (->> (intersections sp1 sp2)
- (map distance)
- sort
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement