-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab09.py
196 lines (180 loc) · 6.74 KB
/
lab09.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class Robot:
"""
Represents a robot and all of its atributions
"""
def __init__(self, room):
self.room = room
self.prev_pos = (0, 0)
self.current_pos = (0, 0)
self.mode_switch_pos = (0, 0)
self.mode = "scan"
self.direction = "r"
def start(self):
"""
Main loop that takes care of matching modes and running them
"""
self.__print()
while self.mode != "end":
match self.mode:
case "scan":
self.scan()
case "clean":
self.mode_switch_pos = self.current_pos
self.clean()
case "back":
self.back_to_scanning()
case "finish_up":
self.finish_up()
def get_dirty_position(self):
"""
Gets and returns next dirty position.
Returns (-1,-1) if not found
"""
if self.current_pos[1] > 0:
if self.room[self.current_pos[0]][self.current_pos[1] - 1] == "o":
return (self.current_pos[0], self.current_pos[1] - 1)
if self.current_pos[0] > 0:
if self.room[self.current_pos[0] - 1][self.current_pos[1]] == "o":
return (self.current_pos[0] - 1, self.current_pos[1])
if self.current_pos[1] < len(self.room[0]) - 1:
if self.room[self.current_pos[0]][self.current_pos[1] + 1] == "o":
return (self.current_pos[0], self.current_pos[1] + 1)
if self.current_pos[0] < len(self.room) - 1:
if self.room[self.current_pos[0] + 1][self.current_pos[1]] == "o":
return (self.current_pos[0] + 1, self.current_pos[1])
return (-1, -1) # no dirty positions found
def __print(self):
for line in self.room:
print(" ".join(line))
print()
def __update_and_print(self):
"""
Updates the robot position and calls map printing
"""
self.room[self.current_pos[0]][self.current_pos[1]] = "r"
self.room[self.prev_pos[0]][self.prev_pos[1]] = "."
self.__print()
def move_to_next_pos(self):
"""
Matches direction and then updates robot position
"""
self.prev_pos = self.current_pos
match self.direction:
case "r":
if self.current_pos[1] == len(self.room[0]) - 1:
self.current_pos = (
self.current_pos[0] + 1, self.current_pos[1])
self.direction = "l"
else:
self.current_pos = (
self.current_pos[0], self.current_pos[1] + 1)
case "l":
if self.current_pos[1] == 0:
self.current_pos = (self.current_pos[0] + 1, 0)
self.direction = "r"
else:
self.current_pos = (
self.current_pos[0], self.current_pos[1] - 1)
case "u":
self.current_pos = (
self.current_pos[0] - 1, self.current_pos[1])
case "d":
self.current_pos = (
self.current_pos[0] + 1, self.current_pos[1])
self.__update_and_print()
def __get_relative_direction(self, dirt_pos):
if dirt_pos[0] < self.current_pos[0]:
return "u"
if dirt_pos[0] > self.current_pos[0]:
return "d"
if dirt_pos[1] < self.current_pos[1]:
return "l"
if dirt_pos[1] > self.current_pos[1]:
return "r"
def __is_dirt_on_the_way(self):
"""
Verifies if there is dirt in the robot's path
"""
if self.current_pos == (self.mode_switch_pos[0],
self.mode_switch_pos[1]
+ 1) or self.current_pos == (
self.mode_switch_pos[0],
self.mode_switch_pos[1] - 1):
return True
if (self.current_pos[0] % 2 == 0
and self.current_pos[0] == self.mode_switch_pos[0] + 1
and (self.current_pos[1] == 0
and self.mode_switch_pos[1] == 0)):
return True
if (self.mode_switch_pos[0] % 2 == 0
and self.current_pos[0] == self.mode_switch_pos[0] + 1
and (self.current_pos[1] == len(self.room[0]) - 1
and self.mode_switch_pos[1] == len(self.room[0]) - 1)):
return True
return False
def finish_up(self):
"""
Last mode
"""
if self.current_pos == (len(self.room) - 1, 0):
self.direction = "r"
self.scan()
else:
self.mode = "end"
def clean(self):
"""
Cleaning mode -> runs while there is dirt to clean
"""
while True:
dirt_pos = self.get_dirty_position()
if dirt_pos == (-1, -1):
self.mode = "back"
break
self.direction = self.__get_relative_direction(dirt_pos)
self.move_to_next_pos()
if self.__is_dirt_on_the_way():
self.mode = "scan"
break
def scan(self):
if self.mode == "scan":
self.direction = "l"
if self.current_pos[0] % 2 == 0:
self.direction = "r"
while True:
if self.get_dirty_position() != (-1, -1):
self.mode = "clean"
break
if self.current_pos[0] == len(self.room) - 1:
if ((self.current_pos[1] == 0 and self.direction == "l")
or (self.current_pos[1] == len(self.room[0]) - 1
and self.direction == "r")):
self.mode = "finish_up"
break
self.move_to_next_pos()
def back_to_scanning(self):
while self.current_pos[1] != self.mode_switch_pos[1]:
if self.get_dirty_position() != (-1, -1):
self.mode = "clean"
return
if self.current_pos[1] > self.mode_switch_pos[1]:
self.direction = "l"
else:
self.direction = "r"
self.move_to_next_pos()
while self.current_pos[0] != self.mode_switch_pos[0]:
if self.get_dirty_position() != (-1, -1):
self.mode = "clean"
return
if self.current_pos[0] > self.mode_switch_pos[0]:
self.direction = "u"
else:
self.direction = "d"
self.move_to_next_pos()
self.mode = "scan"
def main() -> None:
num_lines = int(input())
room = [input().split(" ") for _ in range(num_lines)]
rob = Robot(room)
rob.start()
if __name__ == "__main__":
main()