-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path1041-robot-bounded-in-circle.js
More file actions
54 lines (52 loc) · 1.31 KB
/
1041-robot-bounded-in-circle.js
File metadata and controls
54 lines (52 loc) · 1.31 KB
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
// dir can be "N", "W", "S", "E"
const getNewVector = (move, dir, x, y) => {
let newDir = dir
let newX = x
let newY = y
switch(move) {
case "G":
if(dir === "N") newY++
else if(dir === "W") newX--
else if(dir === "S") newY--
else if(dir === "E") newX++
break
case "L":
if(dir === "N") newDir = "W"
else if(dir === "W") newDir = "S"
else if(dir === "S") newDir = "E"
else if(dir === "E") newDir = "N"
break
case "R":
if(dir === "N") newDir = "E"
else if(dir === "W") newDir = "N"
else if(dir === "S") newDir = "W"
else if(dir === "E") newDir = "S"
break
}
return {
dir: newDir,
x: newX,
y: newY
}
}
/**
* @param {string} instructions
* @return {boolean}
*/
var isRobotBounded = function(instructions) {
// Approach: loop through the instructions and calculate the (x,y)
// after executing the move. Then return true if final (x,y) is (0,0).
// false otherwise
// initial conditions
let x = 0
let y = 0
let dir = "N"
for(let i=0; i<instructions.length; i++) {
const move = instructions[i]
const newVector = getNewVector(move, dir, x, y)
x = newVector.x
y = newVector.y
dir = newVector.dir
}
return (x===0 && y===0) || (dir !== "N")
};