Skip to content

Instantly share code, notes, and snippets.

@cjlarose
Created June 30, 2016 15:26
Show Gist options
  • Save cjlarose/68b31d8824f71c1f1a6c9e24e7a41f39 to your computer and use it in GitHub Desktop.
Save cjlarose/68b31d8824f71c1f1a6c9e24e7a41f39 to your computer and use it in GitHub Desktop.
(ns robot-sim.core)
;; robot = { :direction [0 1], :position [4, 5] }
(defn rotate-right [[x y]]
[y (- x)])
(defn rotate-left [[x y]]
[(- y) x])
(defn dir->displacement-vector [dir]
(case dir
:N [0 1]
:S [0 -1]
:W [-1 0]
:E [1 0]))
(defn move-robot [robot instruction]
(case instruction
\R (update robot :direction rotate-right)
\L (update robot :direction rotate-left)
\A (update robot :position (partial mapv + (:direction robot)))))
(defn main [initial-position initial-direction instructions]
(let [robot { :direction (dir->displacement-vector initial-direction)
:position initial-position }]
(reduce move-robot robot instructions)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment