Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns manhattan.core
- (:require [clojure.main :refer [main]]
- [clojure.string :refer [lower-case split split-lines]]))
- (def pos
- {:u inc :d dec
- :r inc :l dec})
- (defn path-inc [cord [k v]]
- (let [dir (if (contains? #{:u :d} k) 1 0)
- choose #(update % dir (pos k))]
- (take v (rest (iterate choose cord)))))
- (defn trace-line [start dirs]
- (let [llast #(last (last %))
- reducer (fn [acc dir]
- (conj acc (path-inc (llast acc) dir)))]
- (->> dirs
- (reduce reducer [[start]])
- flatten
- (partition 2)
- rest)))
- (defn intersections [line-a line-b]
- (for [[ax ay] line-a
- [bx by] line-b
- :when (and (= ax bx) (= ay by))]
- [ax ay]))
- (defn mh-dist [[ax ay] [bx by]]
- (+ (Math/abs (- ax bx))
- (Math/abs (- ay by))))
- (defn min-dist [start dists]
- (->> dists
- (map (partial mh-dist start))
- (apply min)))
- (def parse-list
- (partial
- map
- (fn [[d & num]]
- [(keyword (lower-case d))
- (Integer/parseInt (apply str num))])))
- (defn parse-input [string]
- (let [[a b] (->> string
- split-lines
- (map #(split % #",")))]
- [(parse-list a) (parse-list b)]))
- (defn -main []
- (let [data (parse-input
- (slurp "input.txt"))
- [a b] (map (partial trace-line [0 0]) data)
- dist (min-dist [0 0] (intersections a b))]
- (println dist)))
- (main (-main))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement