-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarsRovers.py
76 lines (61 loc) · 2.13 KB
/
MarsRovers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __add__(self, point):
return Point(self.x + point.x, self.y + point.y)
def __eq__(self, point):
return self.x == point.x and self.y == point.y
class Plateau:
def __init__(self, maxX, maxY, minX = 0, minY = 0):
self.maxX = maxX
self.minX = minX
self.maxY = maxY
self.minY = minY
def isMovementPossible(self, point):
return self.minX <= point.x <= self.maxX and self.minY <= point.y <= self.maxY
MOVEMENT_VECTORS = {
"N": Point(0,1),
"S": Point(0,-1),
"E": Point(1,0),
"W": Point(-1,0)
}
class MarsRover:
def __init__(self, position, direction, surface):
self.position = position
self.direction = direction
self.surface = surface
def __str__(self):
directionString = ""
for key in MOVEMENT_VECTORS.keys():
if MOVEMENT_VECTORS[key] == self.direction:
directionString = key
return str(self.position.x) + ' ' + str(self.position.y) + ' ' + directionString
def turnRight(self):
self.direction = Point(self.direction.y, -self.direction.x)
def turnLeft(self):
self.direction = Point(-self.direction.y, self.direction.x)
def move(self):
if self.surface.isMovementPossible(self.position + self.direction):
self.position = self.position + self.direction
def parse(input):
output = ''
lines = input.split('\n')
plateauStr = lines[0]
lines = lines[1:]
plateau = Plateau(int(plateauStr[0]),int(plateauStr[2]))
i=0
while(i<len(lines)):
position = Point(int(lines[i][0]),int(lines[i][2]))
direction = MOVEMENT_VECTORS[lines[i][4]]
marsRover = MarsRover(position, direction, plateau)
for instruction in lines[i+1]:
if instruction == 'M':
marsRover.move()
if instruction == 'R':
marsRover.turnRight()
if instruction == 'L':
marsRover.turnLeft()
output += str(marsRover) + '\n'
i += 2
return output