Skip to content

Instantly share code, notes, and snippets.

@positron
Created December 7, 2016 23:06
Show Gist options
  • Save positron/aabac8bd422f9485bb249127c69b8dbb to your computer and use it in GitHub Desktop.
Save positron/aabac8bd422f9485bb249127c69b8dbb to your computer and use it in GitHub Desktop.
(defn N [[x y] distance] [x (+ y distance)])
(defn E [[x y] distance] [(+ x distance) y])
(defn S [[x y] distance] [x (- y distance)])
(defn W [[x y] distance] [(- x distance) y])
(def cardinal-directions [N E S W])
(defn apply-turn [current-direction turn]
"Direction is represented by the int index into `cardinal-directions`"
(let [incremement (if (= turn \R) 1 3)]
(mod (+ current-direction incremement) 4)))
(defn apply-instruction [pos direction distance]
((nth cardinal-directions direction) pos distance))
(defn asdf
[stream]
(loop [pos [0 0] direction 0 stream stream]
(if-let [instruction (first stream)]
(let [turn (first instruction)
distance (Integer/parseInt (apply str (rest instruction)))
new-direction (apply-turn direction turn)
new-pos (apply-instruction pos new-direction distance)]
(println (str new-direction " " new-pos))
(recur new-pos new-direction (rest stream)))
pos)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment