Skip to content

Commit 2ee578c

Browse files
authored
Update 1041-robot-bounded-in-circle.js
1 parent 4e78b3d commit 2ee578c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

1041-robot-bounded-in-circle.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,25 @@ const isRobotBounded = function(instructions) {
1212
}
1313
return x == 0 && y == 0 || i > 0;
1414
};
15+
16+
// another
17+
18+
/**
19+
* @param {string} instructions
20+
* @return {boolean}
21+
*/
22+
const isRobotBounded = function(instructions) {
23+
let x = 0, y = 0, i = 0
24+
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]] // U, R, D, L
25+
for(let e of instructions) {
26+
if(e === 'R') {
27+
i = (i + 1) % 4
28+
} else if(e === 'L') {
29+
i = (i + 3) % 4
30+
} else {
31+
x += dirs[i][0]
32+
y += dirs[i][1]
33+
}
34+
}
35+
return x === 0 && y === 0 || i > 0
36+
};

0 commit comments

Comments
 (0)