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.159 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.158 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.157 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.156 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.155 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.154 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.153 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.152 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.151 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.150 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.149 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.148 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.147 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.146 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.145 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.144 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.143 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.142 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.141 tracks/clojure/exercises/robot-simulator/src/example.clj
trackler-2.2.1.140 tracks/clojure/exercises/robot-simulator/src/example.clj