-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrobot.py
More file actions
139 lines (125 loc) · 5.43 KB
/
robot.py
File metadata and controls
139 lines (125 loc) · 5.43 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Solution(object):
def robotSim(self, commands, obstacles):
"""
:type commands: List[int]
:type obstacles: List[List[int]]
:rtype: int
"""
# This is a very uncleaned logic - Pending on working for cleaned up logic....
# Dictionary Logic
obstacle = {}
for obs in obstacles:
obstacle[(obs[0], obs[1])] = 1
#print obstacle
# Logic works but time limited exceeded for some reason, Reason: Searching through a big list is time expenive, so convert obstacle into a dictionary - logic above fixes this
# Logic2:
# * Have a direction logic - to decide on facing direction
# * Obstacle detecting logic
# Initial origin where the robot would start from
x = 0
y = 0
# Direction logic
# Facing +y direction initially (Initial direction along the +y axis)
facing_direction = "y"
euc_distance = 0
# Go through each command
for com in commands:
# Direction logic
# Move only when it is not left or right turn else update last turn
if com == -1 or com == -2:
if facing_direction == "y":
if com == -1:
facing_direction = "x"
elif com == -2:
facing_direction = "-x"
elif facing_direction == "-y":
if com == -1:
facing_direction = "-x"
elif com == -2:
facing_direction = "x"
elif facing_direction == "x":
if com == -1:
facing_direction = "-y"
elif com == -2:
facing_direction = "y"
elif facing_direction == "-x":
if com == -1:
facing_direction = "y"
elif com == -2:
facing_direction = "-y"
else:
# Robot starts moving in particular direction from last turn
for i in range(com):
# Increment movement
if "x" in facing_direction:
if "-" in facing_direction:
if (x-1, y) not in obstacle:
x -= 1
euc_distance = max(euc_distance, x**2 + y**2)
else:
break
else:
if (x+1, y) not in obstacle:
x += 1
euc_distance = max(euc_distance, x**2 + y**2)
else:
break
else:
if "-" in facing_direction:
if (x, y-1) not in obstacle:
y -= 1
euc_distance = max(euc_distance, x**2 + y**2)
else:
break
else:
if (x, y+1) not in obstacle:
y += 1
euc_distance = max(euc_distance, x**2 + y**2)
else:
break
"""
# While the robot is moving check for obstacles on the way
if [x, y] in obstacles:
if "x" in facing_direction:
if "-" in facing_direction:
x += 1
else:
x -= 1
else:
if "-" in facing_direction:
y += 1
else:
y -= 1
break
"""
return euc_distance
# Logic1: Image a x-y axis graph with 4 quadrants, start at 0,0 (maintain x and y as we proceed), stop at obstacle and continue with the next step
"""
# Initial origin where the robot would start from
x = 0
y = 0
# This logic fails when all the quadrants are exercised, changing
# direction keys as a dictionary for reference (given data from question)
# x,y|-x,-y|x,-y|-x,y ==> select x,y
direction = {
-1:x, # right --> x
-2:y, # left --> y
}
# Track the last direction turn made
last_turn = -2
# Go through each command
for com in commands:
# Move only when it is not left or right turn else update last turn
if com in direction.keys():
last_turn = com
else:
# Robot starts moving in particular direction from last turn
for i in range(com):
direction[last_turn] += 1
# While the robot is moving check for obstacles on the way
if [direction[-1], direction[-2]] in obstacles:
direction[last_turn] -= 1
break
print direction[-1], direction[-2]
return direction[-1]**2 + direction[-2]**2
"""