Created
December 18, 2011 22:49
-
-
Save abp/1494722 to your computer and use it in GitHub Desktop.
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 mars-rovers.core) | |
(def dirs [:N :E :S :W]) | |
(def move-dir {:N [0, 1] :E [1, 0] :S [0, -1] :W [-1, 0]}) | |
(defn turn [dir dirs] | |
(second | |
(drop-while | |
(complement #(= % dir)) | |
(cycle dirs)))) | |
(defn turn-left [dir] | |
(turn dir (reverse dirs))) | |
(defn turn-right [dir] | |
(turn dir dirs)) | |
(defn move [x y dir] | |
(conj | |
(vec | |
(map + [x y] | |
(move-dir dir))) | |
dir)) | |
(defn rover-action [[x y dir] action] | |
(cond | |
(= action \L) [x y (turn-left dir)] | |
(= action \R) [x y (turn-right dir)] | |
(= action \M) (move x y dir))) | |
(defn deploy-rover [start path] | |
(reduce rover-action start path)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment