-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReto#13.py
38 lines (34 loc) · 951 Bytes
/
Reto#13.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
def is_robot_back(moves: str) -> bool | list[int]:
newmoves = ""
i = x = y = 0
changes = {"U": "D", "D": "U", "R": "L", "L": "R"}
while i < len(moves):
move = moves[i]
if move == "*":
newmoves += moves[i + 1]
elif move == "!":
tmp = moves[i + 1]
newmoves += changes[tmp]
i += 1
else:
newmoves += move
i += 1
while "?" in newmoves:
idx = newmoves.index("?")
v = newmoves[idx : idx + 2]
tmp = newmoves[:idx]
if v[1] not in tmp:
newmoves = newmoves.replace(v, v[1])
else:
newmoves = newmoves.replace(v, "")
for m in newmoves:
match m:
case "R":
y += 1
case "L":
y -= 1
case "U":
x += 1
case "D":
x -= 1
return x == 0 and y == 0 or [y, x]