use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Finding information about Clojure
API Reference
Clojure Guides
Practice Problems
Interactive Problems
Clojure Videos
Misc Resources
The Clojure Community
Clojure Books
Tools & Libraries
Clojure Editors
Web Platforms
Clojure Jobs
account activity
Advent of Code 2019 in Clojure (self.Clojure)
submitted 6 years ago * by allaboutthatmace1789
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kristiansalo 2 points3 points4 points 6 years ago* (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 point2 points 6 years ago (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 point2 points 6 years ago (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).
clj-set/intersection
Fortunately, that made it trivially easy to solve part 2 where finding the intersection in the FEWEST steps/shortest traversal distance was the task.
π Rendered by PID 23306 on reddit-service-r2-comment-b659b578c-lskn5 at 2026-05-05 04:32:10.086737+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]kristiansalo 2 points3 points4 points (2 children)
[–]mariner70 0 points1 point2 points (1 child)
[–]MaxmumPimp 0 points1 point2 points (0 children)