Skip to content

Commit faec736

Browse files
authored
Update 1041-robot-bounded-in-circle.js
1 parent b11a4f9 commit faec736

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1041-robot-bounded-in-circle.js

+23
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,26 @@ const isRobotBounded = function(instructions) {
3434
}
3535
return x === 0 && y === 0 || i > 0
3636
};
37+
38+
// another
39+
40+
/**
41+
* @param {string} instructions
42+
* @return {boolean}
43+
*/
44+
const isRobotBounded = function(instructions) {
45+
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]
46+
let x = 0, y = 0, i = 0
47+
for(let ins of instructions) {
48+
if(ins === 'G') {
49+
const dir = dirs[i]
50+
x += dir[0]
51+
y += dir[1]
52+
} else if(ins === 'L') {
53+
i = i - 1 < 0 ? 3 : i - 1
54+
} else if(ins === 'R') {
55+
i = i + 1 > 3 ? 0 : i + 1
56+
}
57+
}
58+
return x === 0 && y === 0 || i !== 0
59+
};

0 commit comments

Comments
 (0)