Sha256: 8c1779111f714cb79811ec62990b3aab8c6144d7514bc13b76300a832d280511

Contents?: true

Size: 1.41 KB

Versions: 396

Compression:

Stored size: 1.41 KB

Contents

(ns robot-simulator)

(def directions [:north :east :south :west])

(defn robot [coordinates bearing]
  {:coordinates coordinates :bearing bearing})

(defn turn [bearing direction-list]
  (let [dir-stream (drop-while #(not (= bearing %1)) (cycle direction-list))]
    (nth dir-stream 1)))

(defn turn-right [bearing]
  (turn bearing directions))

(defn turn-left [bearing]
  (turn bearing (reverse directions)))

(defn advance [coordinates bearing]
  (let [x (:x coordinates)
        y (:y coordinates)]
    (cond
      (= :north bearing) {:x x :y (inc y)}
      (= :south bearing) {:x x :y (dec y)}
      (= :east  bearing) {:x (inc x) :y y}
      (= :west  bearing) {:x (dec x) :y y})))

(defn simulate [instructions current-state]
  (loop [instructions  instructions
         current-state current-state]
    (let [instruction (first instructions)
          remainder   (rest  instructions)
          coordinates (:coordinates current-state)
          bearing     (:bearing current-state)
          next-state  (cond
                           (= \L instruction)
                             (robot coordinates (turn-left bearing))
                           (= \R instruction)
                             (robot coordinates (turn-right bearing))
                           :else
                             (robot (advance coordinates bearing) bearing))]
      (if (seq remainder)
        (recur remainder next-state)
        next-state))))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.179 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.178 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.177 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.176 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.175 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.174 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.173 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.172 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.171 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.170 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.169 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.167 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.166 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.165 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.164 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.163 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.162 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.161 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.160 tracks/clojure/exercises/robot-simulator/src/example.clj