Skip to content

Commit cee0441

Browse files
authored
Create 2069-walking-robot-simulation-ii.js
1 parent b29f91d commit cee0441

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

2069-walking-robot-simulation-ii.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {number} width
3+
* @param {number} height
4+
*/
5+
const Robot = function(width, height) {
6+
this.i = 0
7+
const pos = Array()
8+
this.len = width + height - 1 + width - 1 + height - 2
9+
pos.push( [0,0,3] )
10+
for(let i = 1; i < width; i++) {
11+
pos.push([i, 0, 0])
12+
}
13+
for(let i = 1; i < height; i++) {
14+
pos.push([width - 1, i, 1])
15+
}
16+
for(let i = 1; i < width; i++) {
17+
pos.push([width - 1 - i, height - 1, 2])
18+
}
19+
for(let i = 1; i < height - 1; i++) {
20+
pos.push([0, height - 1 - i, 3])
21+
}
22+
this.pos = pos
23+
};
24+
25+
/**
26+
* @param {number} num
27+
* @return {void}
28+
*/
29+
Robot.prototype.step = function(num) {
30+
this.i += num
31+
};
32+
33+
/**
34+
* @return {number[]}
35+
*/
36+
Robot.prototype.getPos = function() {
37+
return this.pos[this.i % this.len].slice(0, 2)
38+
};
39+
40+
/**
41+
* @return {string}
42+
*/
43+
Robot.prototype.getDir = function() {
44+
const hash = ['East', 'North', 'West', 'South']
45+
if(this.i === 0) return hash[0]
46+
else {
47+
return hash[this.pos[this.i % this.len][2]]
48+
}
49+
};

0 commit comments

Comments
 (0)