Created
June 30, 2016 15:26
-
-
Save cjlarose/68b31d8824f71c1f1a6c9e24e7a41f39 to your computer and use it in GitHub Desktop.
Robot Sim in Clojure (https://github.com/SanDiegoKats/katas/tree/master/2016-06-29)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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