Saturday, June 12, 2010

Mars Rover solution in Clojure

The Mars Rover problem has been solved by other people earlier:

Veera Sundar - Java

Arun Ravindran - Python and Haskell

Baishampayan Ghose - Clojure

A simple and less concise Clojure solution is listed in this post.

Mars Rover Problem

A squad of robotic rovers are to be landed by NASA on a plateau on Mars.

This plateau, which is curiously rectangular, must be navigated by the rovers so that their on-board cameras can get a complete view of the surrounding terrain to send back to Earth.

A rover’s position and location is represented by a combination of x and y co-ordinates and a letter representing one of the four cardinal compass points. The plateau is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the rover is in the bottom left corner and facing North.

In order to control a rover , NASA sends a simple string of letters. The possible letters are ‘L’, ‘R’ and ‘M’. ‘L’ and ‘R’ makes the rover spin 90 degrees left or right respectively, without moving from its current spot. ‘M’ means move forward one grid point, and maintain the same heading.

Assume that the square directly North from (x, y) is (x, y+1).


The first line of input is the upper-right coordinates of the plateau, the lower-left coordinates are assumed to be 0,0.

The rest of the input is information pertaining to the rovers that have been deployed. Each rover has two lines of input. The first line gives the rover’s position, and the second line is a series of instructions telling the rover how to explore the plateau.

The position is made up of two integers and a letter separated by spaces, corresponding to the x and y co-ordinates and the rover’s orientation.

Each rover will be finished sequentially, which means that the second rover won’t start to move until the first one has finished moving.


The output for each rover should be its final co-ordinates and heading.


Test Input:

5 5
1 2 N
3 3 E

Expected Output:

1 3 N
5 1 E

Clojure Solution

(ns marsrover.main)

(defstruct rover-location :x :y :facing)

(def facing-all [:north :east :west :south])

(def facing-lookup (zipmap facing-all "NEWS"))

(def direction-lookup (zipmap "NEWS" facing-all))

(def go-left-lookup (zipmap facing-all [:west :north :south :east]))

(def go-right-lookup (zipmap facing-all [:east :south :north :west]))

(def move-lookup
{:north #(struct-map rover-location :x (% :x) :y (inc (% :y)) :facing :north)
:east #(struct-map rover-location :x (inc (% :x)) :y (% :y) :facing :east)
:south #(struct-map rover-location :x (% :x) :y (dec (% :y)) :facing :south)
:west #(struct-map rover-location :x (dec (% :x)) :y (% :y) :facing :west)}

(defn turn-left
(struct-map rover-location
:x (rloc :x)
:y (rloc :y)
:facing (go-left-lookup (rloc :facing))))

(defn turn-right
(struct-map rover-location
:x (rloc :x)
:y (rloc :y)
:facing (go-right-lookup (rloc :facing))))

(defn move
(let [func (move-lookup (rloc :facing))]
(func rloc)))

(def command-lookup (zipmap "LRM" [turn-left turn-right move]))

(defn process-each-command
[rloc single-cmd]
(let [func (command-lookup single-cmd)]
(func rloc)))

(defn process-commands
[[x y d] cmds]
(loop [rloc (struct-map rover-location :x x :y y :facing (direction-lookup d))
cmd-seq (seq cmds)]
(if (empty? cmd-seq)
(recur (process-each-command rloc (first cmd-seq)) (rest cmd-seq)))))

(defn print-location
(println (str (rloc :x) (rloc :y) (facing-lookup (rloc :facing))))))

;(defn -main
; [input]
(print-location (process-commands [1 2 \N] "LMLMLMLMM"))
(print-location (process-commands [3 3 \E] "MMRMMRMRRM")))
; )

Your comments and feedback are welcome.


  1. Very interesting solution Shantanu. I think you can probably write process-commands in a recursive manner too.

  2. @arocks process-commands is already recursive as I understand. Could you please describe?


Disqus for Char Sequence