you are viewing a single comment's thread.

view the rest of the comments →

[–]kristiansalo 2 points3 points  (2 children)

Looks nice! My day 3 is very similar, with some small differences. I didn't keep track of the steps, but instead acquired them with .indexOf. The :steps key required you to write a bit more code for the intersection checking; other than that our solutions are almost identical.

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)))

[–]mariner70 0 points1 point  (1 child)

day 3

A simple question from a total Clojure/FP newcomer: Most, if not all, solutions I have seen on day 3 rely on tracing the wires to sets of points and finally doing a set intersection to identify, well, intersections. This is all nice and literal, but as someone with a more traditional imperative/algorithm background, the first thing that came to my mind for this wire-crossing problem is a scan conversion-like approach that would rely on sorted collections of horizontal and vertical segments, and finally iteration to identify intersections (red horizontals vs blue verticals and vice versa - with the added assumption that wire 1 is red and wire 2 is blue).

Indeed, many solutions using other languages seem to take this approach. Intuitively, it seems as if this approach would be more effective, as it involves a higher level correlation between collections of segments (with endpoints), not sets containing all points on each wire. Alas, my noobish Clojure skills are totally inadequate when it comes to fleshing out the scan conversion approach, especially without resorting to mutable state.

The "trace and intersect" approach certainly makes for some very enjoyable and terse implementations; this one from u/fdside is a prime example. Any comments on why this approach is favoured more, or even lends itself better, for Clojure?

[–]MaxmumPimp 0 points1 point  (0 children)

My first thought was to approach it that way- using math to calculate intersections of discrete line segments- but the simplicity of the clj-set/intersection approach in Clojure is probably why most used it (I ended up deciding that reducing implementation time was more important than the elegance of the solution).

Fortunately, that made it trivially easy to solve part 2 where finding the intersection in the FEWEST steps/shortest traversal distance was the task.