DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 3: Crossed Wires

Collapse
 
salokristian profile image
salokristian

Here's my solution with Clojure. It is generating all the points in the lines, which seems a bit brute-force, but at least on my machine it works swiftly;)

I'm also posting my solutions to github.

(defn parse-wire [s]
  (->> (str/split s #",")
       (map (fn [s]
              {:dir   (subs s 0 1)
               :steps (edn/read-string (subs s 1))}))))

(defn line-points [start-point line]
  (let [step-line (case (:dir line)
                    "U" #(update start-point :y + %)
                    "D" #(update start-point :y - %)
                    "R" #(update start-point :x + %)
                    "L" #(update start-point :x - %))]
    (->> (:steps line)
         inc
         (range 1)
         (map step-line))))

(defn create-grid [wire]
  (reduce (fn [grid line]
            (into grid (line-points (last grid) line)))
          [{:x 0 :y 0}]
          wire))

(def wires (common/parse-file parse-wire "day_3.txt"))
(def grid-1 (create-grid (first wires)))
(def grid-2 (create-grid (second wires)))
(def intersect-points (clj-set/intersection (set grid-1) (set grid-2)))

(defn part-1 []
  (->> intersect-points
       (map #(+ (Math/abs (:x %)) (Math/abs (:y %))))
       (filter #(not (zero? %)))
       (apply min)))

(defn part-2 []
  (->> intersect-points
       (map #(+ (.indexOf grid-1 %) (.indexOf grid-2 %)))
       (filter #(not (zero? %)))
       (apply min)))
Collapse
 
simonced profile image
simonced

Wow, your solution is so compact!
Mine is very verbose, and I am not done with part 2 yet! (will try tonight, but it's hard when working 11h a day...).

Maybe you could have a look on my solution and give me some pointers on how I could get better at Clojure?

gitlab.com/simonced/advent-of-code...

I am just beginning and I enjoy the language very much :).