Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn follow [[hx hy] [tx ty]] (let [change {false dec, true inc}] (
- cond
- (every? #{-1 0 1} [(- hx tx) (- hy ty)]) [tx ty]
- (zero? (- hx tx)) [tx ((change (> hy ty)) ty)]
- (zero? (- hy ty)) [((change (> hx tx)) tx) ty]
- :else [((change (> hx tx)) tx) ((change (> hy ty)) ty)]
- )))
- (defn make-step [dir head tail] (let [
- transforms {"R" [inc identity], "U" [identity inc], "D" [identity dec], "L" [dec identity]}
- transform (transforms dir)
- new-head (vec (map #(%1 %2) transform head))
- ] [new-head (follow new-head tail)]))
- (defn travel [[history knots] [dir steps]] (reduce
- (fn [[history knots] _] (let [
- [head sec] (make-step dir (first knots) (second knots))
- new-knots (reduce #(conj %1 (follow (peek %1) %2)) [sec] (nthrest knots 2))
- ] [(conj history (peek new-knots)) (concat [head] new-knots)]))
- [history knots]
- (range steps)
- ))
- (defn do-task [travel] (
- -> travel first distinct count println
- ))
- (with-open [rdr (clojure.java.io/reader "trail.in")] (
- let [
- lines (->> rdr
- line-seq
- (map #(clojure.string/split % #" "))
- (map (fn [[dir steps]] [dir (Integer/parseInt steps)])))
- ] (->> lines
- (reduce travel [[[0 0]] [[0 0] [0 0]]])
- do-task)
- (->> lines
- (reduce travel [[[0 0]] (vec (replicate 10 [0 0]))])
- do-task)
- ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement