There is a common Mars Rover Problem Developers are asked to code against during Interview Process. Last year I went through one such excersize and submitted my solution in Java. It turned out to be 10 Java classes long and almost same number of files.
Now that I am learning Scala, I thought instead of writing the Hello World program in Scala how would it be to design the solution to the same problem. It took me a day to get it right but amazingly the solution was terse one file long with one Scala application class and many Scala object singletons.
Here is the problem and the Scala solution to it.
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).
INPUT: 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.
OUTPUT The output for each rover should be its final co-ordinates and heading.
INPUT AND OUTPUT
Now that I am learning Scala, I thought instead of writing the Hello World program in Scala how would it be to design the solution to the same problem. It took me a day to get it right but amazingly the solution was terse one file long with one Scala application class and many Scala object singletons.
Here is the problem and the Scala solution to it.
The 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).
INPUT: 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.
OUTPUT The output for each rover should be its final co-ordinates and heading.
INPUT AND OUTPUT
Test Input:
5 5
1 2 N
LMLMLMLMM
3 3 E
MMRMMRMRRM
Expected Output:1 3 N
5 1 E
object RoverApplication { sealed abstract class Direction(val dx:Int, val dy: Int, left: => Direction) { lazy val turnLeft = left lazy val turnRight = turnLeft.turnLeft.turnLeft } case object North extends Direction(0, 1, West) case object West extends Direction(-1, 0, South) case object South extends Direction(0,-1, East) case object East extends Direction(1,0, North) val directions = List(North, West, South, East) case class Position(x: Int, y: Int) { def moveBy(dir:Direction) = copy(x + dir.dx, y + dir.dy) override def toString = "(%d/%d)".format(x, y) } case class Rover(position: Position, direction: Direction) { def moveForward = copy(position moveBy direction) def turnLeft = copy(direction = direction.turnLeft) def turnRight = copy(direction = direction.turnRight) val commands = Map('M' -> moveForward _, 'L' -> turnLeft _, 'R' -> turnRight _) def move(directions: String): Rover = directions.foldLeft(this){ _.commands(_).apply } override def toString = "Rover is at %s, looking %s".format(position, direction) } def main(args: Array[String]) { val input = List("5 5", "1 2 N", "LMLMLMLMM", "3 3 E", "MMRMMRMRRM") val Array(plateauHight, plateauWidth) = input.head split ' ' val roversAndCommands = for (List(orientation, commands) <- input.tail grouped 2) yield { val Array(x, y, d) = orientation split ' ' val position = Position(x.toInt, y.toInt) (Rover(position, directions.find(_.toString.startsWith(d)).get), commands) } for ((rover, commands) <- roversAndCommands) println(rover move commands) } }
No comments:
Post a Comment