I have a grid and a class
Vehicle
, which will have starting point(X, Y on the grid) and direction(one of N,E,S,W) taken from user and there will be commands,L
&R
turns the vehicle 90 degrees around left and right respectively andM
moves the vehicle one unit to faced direction.For instance, if the command is
M
and the current direction isE
, the vehicle moves from(x,y)
to(x+1,y)
and preserve its direction.
If the command isR
, and the current direction isE
, vehicle preserves its position but direction changes toS
.My input consists of 5 lines:
- The first line defines the limits of the grid; X and Y separated by space
- The second line defines the current position and facing direction for the first vehicle; X, Y and Dir are separated by space
- The third line defines the commands for the first vehicle, which is a line of string
- The fourth and fifth lines are the same as second and third except they are for the second vehicle
Note: Vehicles are sent sequentially. If the second vehicle attempts to move to the occupied spot of the first vehicle, the command will be skipped. If any move command makes any of the vehicles move out of the grid, that command will be skipped as well. Inputs are always in expected style, so there is no need to validate inputs.
As an example:
Inputs:
6 6
1 3 E
RMLLMRMRM
1 1 N
LMLML
Output:
2 2 S
0 0 E
My code:
directions = ['N','E','S','W']
movement = {'N': (0,1), 'E': (1,0), 'S': (0,-1), 'W':(-1,0)}
commands = {'L': 'turn_left', 'R': 'turn_right', 'M': 'move'}
GRID_MAX_X, GRID_MAX_Y = map(int, raw_input().split())
first_vehicle_x = None
first_vehicle_y = None
class Vehicle():
def __init__(self, x, y, face):
self.x = x
self.y = y
self.dir = face
def turn_left(self):
self.dir = directions[(directions.index(self.dir)-1)%len(directions)]
def turn_right(self):
self.dir = directions[(directions.index(self.dir)+1)%len(directions)]
def move(self):
new_x = self.x + movement[self.dir][0]
new_y = self.y + movement[self.dir][1]
if new_x != first_vehicle_x or new_y != first_vehicle_y:
if new_x in xrange(GRID_MAX_X+1):
self.x = new_x
if new_y in xrange(GRID_MAX_Y+1):
self.y = new_y
vehicle_one_pos = raw_input().split()
vehicle_one_commands = raw_input()
vehicle_one = Vehicle(int(vehicle_one_pos[0]), int(vehicle_one_pos[1]), vehicle_one_pos[2])
for command in vehicle_one_commands:
eval("vehicle_one.{0}()".format(commands[command]))
first_vehicle_x = vehicle_one.x
first_vehicle_y = vehicle_one.y
vehicle_two_pos = raw_input().split()
vehicle_two_commands = raw_input()
vehicle_two = Vehicle(int(vehicle_two_pos[0]), int(vehicle_two_pos[1]), vehicle_two_pos[2])
for command in vehicle_two_commands:
eval("vehicle_two.{0}()".format(commands[command]))
print vehicle_one.x, vehicle_one.y, vehicle_one.dir
print vehicle_two.x, vehicle_two.y, vehicle_two.dir
About my code, it feels like repetition on doing same things for each vehicle but wasnt't sure how to handle without it since I need position of vehicle_one
for vehicle_two
.
Any comment is always appreciated, thanks in advance.