Skip to content

Instantly share code, notes, and snippets.



Created Dec 16, 2019
What would you like to do?
(:require [clojure.set :as set]))
(def directions {"U" [0 -1]
"D" [0 1]
"L" [-1 0]
"R" [1 0]})
(defn input-item-to-list [input-item]
(let [[input-again direction count] (re-matches #"(.)(\d*)" input-item)
count (Integer. count)]
(repeat count
(directions direction))))
(defn input-items-to-list [input-items]
(mapcat input-item-to-list input-items))
;;copy, put as strings, e.g. (def w1input (input-items-to-list ["R1003","U476","L876","U147"]))
;;This does require manipulation of the input data, but that a choice for speed.
(def w1input (input-items-to-list []))
(def w2input (input-items-to-list []))
(defn positions
"Given a set of STEPS (like [[0 1] [-1 0]]), return all positions taken by those steps.
The result from the example is [[0 0] [0 1] [-1 1]]."
(into #{}
(reductions (partial map +)
[0 0]
(def w1positions (positions w1input))
(def w2positions (positions w2input))
(def common-positions
(set/intersection w1positions w2positions))
(defn manhattan-distance
(manhattan-distance [0 0] position))
([[p1x p1y :as position1]
[p2x p2y :as position2]]
(+ (Math/abs (- p2x p1x))
(Math/abs (- p2y p1y)))))
(def p1-solution
(apply min (map manhattan-distance (disj common-positions [0 0]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment