Skip to content

Instantly share code, notes, and snippets.

@4mitch
Created December 15, 2019 22:13
Show Gist options
  • Save 4mitch/4c5a48ba3642e9177514578ba7f18f14 to your computer and use it in GitHub Desktop.
Save 4mitch/4c5a48ba3642e9177514578ba7f18f14 to your computer and use it in GitHub Desktop.
Advent of Code '19 day 3 part 1
(defn step [s]
[(keyword (subs s 0 1)) (Integer/parseInt (subs s 1))])
(defn get-dots [dots action]
(let [[op len] action
dots dots
from (last dots)
[from_x from_y] from
f+x (partial + from_x 1)
f-x (partial - from_x 1)
f+y (partial + from_y 1)
f-y (partial - from_y 1)]
(case op
:R (map (fn [x] [(f+x x),from_y]) (range len))
:L (map (fn [x] [(f-x x),from_y]) (range len))
:U (map (fn [x] [from_x,(f+y x)]) (range len))
:D (map (fn [x] [from_x,(f-y x)]) (range len)))))
(defn do-step [acc step]
(into acc (get-dots acc step))
)
(let [n3input (clojure.string/split n3input_str #",")
n3input2 (clojure.string/split n3input_str_2 #",")
steps1 (map step n3input)
steps2 (map step n3input2)
dots1 (rest (reduce do-step [[0,0]] steps1))
dots2 (rest (reduce do-step [[0,0]] steps2))
crosses (clojure.set/intersection (set dots1) (set dots2))
]
(sort-by (fn [[x y]] (+ (Math/abs x) (Math/abs y)) ) crosses)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment