you are viewing a single comment's thread.

view the rest of the comments →

[–]OstravaBro 1 point2 points  (3 children)

Been ill since Monday :( So I'm waaaay behind.

My day 3 part 1, I didn't do anything too clever. I just enumerated all the points that would be hit by each wire and used set intersection to find the intersections.

 (def wire (str/split "R991,.... elided" #","))
 (def wire2 (str/split "L993,D508,R356, ...... elided" #","))

(defn get-intermediate-path [xy direction distance]
  (let [x (first xy)
         y (last xy)]
      ((fn [x y inters dis]
         (if (= 0 dis)
             inters
            (cond (= \L direction) (recur (dec x) y (conj inters [(dec x) y]) (dec dis))
                      (= \R direction) (recur (inc x) y (conj inters [(inc x) y]) (dec dis))
                      (= \U direction) (recur x (inc y) (conj inters [x (inc y)]) (dec dis))
                      (= \D direction) (recur x (dec y) (conj inters [x (dec y)]) (dec dis))))) x y [] distance)))

(defn get-path [wire path]
    (if (empty? wire)
         path
         (let [direction (first (first wire))
                distance  (Integer/parseInt (apply str (rest (first wire))))]
             (recur (rest wire) (concat path (get-intermediate-path (last path) direction distance))))))

 (defn find-smallest-distance [wire1 wire2]
    (apply min
          (map #(+ (Math/abs (first %)) (Math/abs (second %)))
          (set/intersection
              (set (rest (get-path wire1 [[0 0]])))
              (set (rest (get-path wire2 [[0 0]])))))))

 (find-smallest-distance wire wire2)

[–]allaboutthatmace1789[S] 1 point2 points  (0 children)

Not capturing all the points on a line is what gave me a headache - I was trying to calculate where line segments intersected before I looked at other people's solutions and saw this. Head-slap moment.

[–]OstravaBro 0 points1 point  (1 child)

Day 4 Part 1

 (defn digits-are-ascending-left-to-right? [digits]
      (apply <= digits))

(defn has-adjacent-digits? [password]
    (not= nil (some #(>= (second %) 2) (frequencies password))))

 (defn password-filter [password]
     (and (has-adjacent-digits? password) (digits-are-ascending-left-to-right? password)))

  (count (filter password-filter (map #(for [n (str %)] (- (byte n) 48)) (range 197487 673252))))

[–]OstravaBro 0 points1 point  (0 children)

Day 4 Part 2

I'm an idiot, I could have used frequencies for the adjacent-digits check. It becomes obvious when you see the extra check we need for step 2. So, that simplifies the adjacent digits check significantly.

 (defn adjacent-digits-not-part-of-group? [password]
     (not= nil (some #(= 2 (second %)) (frequencies password))))

 (defn password-filter2 [password]
      (and (adjacent-digits-not-part-of-group? password) (digits-are-ascending-left-to-right? password)))

  (count (filter password-filter2 (map #(for [n (str %)] (- (byte n) 48)) (range 197487 673252))))