1
var isRobotBounded = function (instructions) {
2
// north = 0, east = 1, south = 2, west = 3
3
let directions = [
4
[0, 1],
5
[1, 0],
6
[0, -1],
7
[-1, 0],
8
];
9
// Initial position is in the center
10
let x = 0,
11
y = 0;
12
// facing north
13
let idx = 0;
14
let movements = [...instructions];
15

16
for (let move of movements) {
17
if (move == "L") idx = (idx + 3) % 4;
18
else if (move == "R") idx = (idx + 1) % 4;
19
else {
20
x += directions[idx][0];
21
y += directions[idx][1];
22
}
23
}
24

25
// after one cycle:
26
// robot returns into initial position
27
// or robot doesn't face north
28
return (x == 0 && y == 0) || idx != 0;
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0