Created
May 16, 2010 19:02
-
-
Save dmitriy-kiriyenko/403092 to your computer and use it in GitHub Desktop.
The solution of Mars Rovers problem. Description is below, in first comment.
This file contains 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
#!/usr/bin/ruby | |
DIRS = {:N => Complex(0, 1), :E => Complex(1, 0), :S => Complex(0, -1), :W => Complex(-1, 0)} | |
OPS = {:R => lambda { |pos, dir| [pos, dir*(-Complex::I)] }, | |
:L => lambda { |pos, dir| [pos, dir*Complex::I] }, | |
:M => lambda { |pos, dir| create_rover(pos + dir, dir) } } | |
X_MIN, Y_MIN = 0, 0 | |
def ensure_between a, min, max | |
[max, [a, min].max].min | |
end | |
def create_rover pos, dir | |
[Complex(ensure_between(pos.real, X_MIN, X_MAX), ensure_between(pos.imag, Y_MIN, Y_MAX)), dir] | |
end | |
def read_pos pos | |
pos_arr = pos.split(" ") | |
create_rover(Complex(pos_arr[0].to_i, pos_arr[1].to_i), DIRS[pos_arr[2].to_sym]) | |
end | |
def process pos, instrs | |
instrs.chars.map(&:to_sym).inject(read_pos pos) { |r, i| OPS[i].call(*r) } | |
end | |
X_MAX, Y_MAX = ARGF.readline.split(" ").map(&:to_i) | |
ARGF.each_slice(2) do |pos, instrs| | |
rover = process *[pos, instrs].map {|s| s.chomp.upcase} | |
puts [rover[0].real.to_s, rover[0].imag.to_s, DIRS.invert[rover[1]]].join(" ") | |
end |
This file contains 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
#!/usr/bin/ruby | |
DIRS = "NESW" | |
DIR_SHIFTS = [[0, 1], [1, 0], [0, -1], [-1, 0]] | |
DIR_COUNT = DIRS.size | |
OPS = {:R => lambda { |x, y, dir| [x, y, (dir + 1) % DIR_COUNT] }, | |
:L => lambda { |x, y, dir| [x, y, (dir - 1 + DIR_COUNT) % DIR_COUNT] }, | |
:M => lambda { |x, y, dir| create_rover(x + DIR_SHIFTS[dir][0], y + DIR_SHIFTS[dir][1], dir) } } | |
X_MIN, Y_MIN = 0, 0 | |
def ensure_between a, min, max | |
[max, [a, min].max].min | |
end | |
def create_rover x, y, dir | |
[ensure_between(x, X_MIN, X_MAX), ensure_between(y, Y_MIN, Y_MAX), dir] | |
end | |
def read_pos pos | |
pos_arr = pos.split(" ") | |
create_rover pos_arr[0].to_i, pos_arr[1].to_i, DIRS.index(pos_arr[2]) | |
end | |
def process pos, instrs | |
instrs.chars.map(&:to_sym).inject(read_pos pos) { |r, i| OPS[i].call(*r) } | |
end | |
X_MAX, Y_MAX = ARGF.readline.split(" ").map(&:to_i) | |
ARGF.each_slice(2) do |pos, instrs| | |
rover = process *[pos, instrs].map { |s| s.chomp.upcase } | |
puts [rover[0].to_s, rover[1].to_s, DIRS[rover[2]]].join(" ") | |
end |
This file contains 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
package dim.mars.rovers; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
public class MarsRovers { | |
static final String dirs = "NESW"; | |
static final int[][] dirShifts = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; | |
static final int dirCount = dirs.length(); | |
static int xMin = 0, yMin = 0, xMax, yMax; | |
static final int X = 0, Y = 1, DIR = 2; | |
static String move(String position, String instructions) { | |
int[] rover = createRover(position.split(" ")); | |
for (final char instruction : instructions.toCharArray()) { | |
rover = singleMove(rover, instruction); | |
} | |
return rover[X] + " " + rover[Y] + " " + dirs.charAt(rover[DIR]); | |
} | |
static private int[] singleMove(int[] rover, char instruction) { | |
switch (instruction) { | |
case 'R': | |
return new int[] { rover[X], rover[Y], (rover[DIR] + 1) % dirCount }; | |
case 'L': | |
return new int[] { rover[X], rover[Y], (rover[DIR] - 1 + dirCount) % dirCount }; | |
case 'M': | |
return createRover(rover[X] + dirShifts[rover[DIR]][X], rover[Y] + dirShifts[rover[DIR]][Y], | |
rover[DIR]); | |
default: | |
return rover; | |
} | |
} | |
static private int[] createRover(String[] positionParts) { | |
return createRover(Integer.parseInt(positionParts[X]), Integer.parseInt(positionParts[Y]), | |
dirs.indexOf(positionParts[DIR])); | |
} | |
private static int[] createRover(int x, int y, int dir) { | |
return new int[] { ensureBetween(x, xMin, xMax), ensureBetween(y, xMin, yMax), dir }; | |
} | |
static private int ensureBetween(int value, int min, int max) { | |
return Math.max(min, Math.min(max, value)); | |
} | |
public static void main(String[] args) throws Exception { | |
final BufferedReader in = new BufferedReader(new FileReader("input.txt")); | |
final String plateau = in.readLine(); | |
final String[] dimensions = plateau == null ? new String[] { "0", "0" } : plateau.split(" "); | |
xMax = Integer.parseInt(dimensions[X]); | |
yMax = Integer.parseInt(dimensions[Y]); | |
String position, instructions; | |
while ((position = in.readLine()) != null && (instructions = in.readLine()) != null) { | |
System.out.println(move(position.toUpperCase(), instructions.toUpperCase())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PROBLEM : MARS ROVERS
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