Skip to content

Instantly share code, notes, and snippets.

@quephird
Created December 9, 2022 18:46
Show Gist options
  • Save quephird/4c730661f9707f04b83f549ae9d8b404 to your computer and use it in GitHub Desktop.
Save quephird/4c730661f9707f04b83f549ae9d8b404 to your computer and use it in GitHub Desktop.
(require ['clojure.string :as 'str])
(def head (atom [0 0]))
(def tail (atom [0 0]))
(def visited-tail-positions (atom #{[0 0]}))
(defn get-x [atom]
(get @atom 0))
(defn get-y [atom]
(get @atom 1))
(defn tail-touching-head? []
(let [head-x (get-x head)
head-y (get-y head)
tail-x (get-x tail)
tail-y (get-y tail)]
(and (>= 1 (abs (- head-x tail-x)))
(>= 1 (abs (- head-y tail-y))))))
(defn move-head-right [units]
(dotimes [_ units]
(swap! head update-in [0] inc)
(if (not (tail-touching-head?))
(let [head-x (get-x head)
head-y (get-y head)]
(reset! tail [(dec head-x) head-y])
(swap! visited-tail-positions conj @tail)))))
(defn move-head-left [units]
(dotimes [_ units]
(swap! head update-in [0] dec)
(if (not (tail-touching-head?))
(let [head-x (get-x head)
head-y (get-y head)]
(reset! tail [(inc head-x) head-y])
(swap! visited-tail-positions conj @tail)))))
(defn move-head-up [units]
(dotimes [_ units]
(swap! head update-in [1] inc)
(if (not (tail-touching-head?))
(let [head-x (get-x head)
head-y (get-y head)]
(reset! tail [head-x (dec head-y)])
(swap! visited-tail-positions conj @tail)))))
(defn move-head-down [units]
(dotimes [_ units]
(swap! head update-in [1] dec)
(if (not (tail-touching-head?))
(let [head-x (get-x head)
head-y (get-y head)]
(reset! tail [head-x (inc head-y)])
(swap! visited-tail-positions conj @tail)))))
(defn move-head [direction units]
(case direction
"R" (move-head-right units)
"L" (move-head-left units)
"U" (move-head-up units)
"D" (move-head-down units)
:else nil))
(defn parse-line [line]
(let [[direction number] (str/split line #" ")]
[direction (Integer/parseInt number)]))
(defn parse-input [filename]
(->> filename
slurp
str/split-lines
(map parse-line)))
(defn solution-part-1 [filename]
(let [all-moves (parse-input filename)]
(doseq [move all-moves]
(apply move-head move))
(count @visited-tail-positions)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment