1
const EAST = "East";
2
const NORTH = "North";
3
const WEST = "West";
4
const SOUTH = "South";
5

6
const DIRECTIONS = [EAST, NORTH, WEST, SOUTH];
7
const ORIGIN = [0, 0];
8

9
/**
10
* @param {number} width
11
* @param {number} height
12
*/
13
var Robot = function (width, height) {
14
this.width = width;
15
this.height = height;
16

17
this.path = []; // circular buffer of entire path of robot
18
this.dirAtSquare = []; // dir robot is facing at each square
19
this.currentPos = -1;
20

21
this.preprocess();
22
};
23

24
/**
25
* Loop around the map once and store the path into a circular buffer.
26
*
27
* (0,0) is set to face SOUTH. The only time it is EAST is at the beginning
28
* if the robot didn't move at all.
29
*
30
* @return {void}
31
*/
32
Robot.prototype.preprocess = function () {
33
// go right
34
for (let i = 1; i < this.width; i++) {
35
this.path.push([i, 0]);
36
this.dirAtSquare.push(0);
37
}
38

39
// go up
40
const xMax = this.width - 1;
41
for (let i = 1; i < this.height; i++) {
42
this.path.push([xMax, i]);
43
this.dirAtSquare.push(1);
44
}
45

46
// go left
47
const yMax = this.height - 1;
48
for (let i = xMax - 1; i >= 0; i--) {
49
this.path.push([i, yMax]);
50
this.dirAtSquare.push(2);
51
}
52

53
// go down
54
const xMin = 0;
55
for (let i = yMax - 1; i >= 0; i--) {
56
this.path.push([xMin, i]);
57
this.dirAtSquare.push(3);
58
}
59
};
60

61
/**
62
* @param {number} num
63
* @return {void}
64
*/
65
Robot.prototype.step = function (num) {
66
this.currentPos += num;
67
};
68

69
/**
70
* @return {number[]}
71
*/
72
Robot.prototype.getPos = function () {
73
return this.currentPos === -1
74
? ORIGIN // we haven't moved
75
: this.path[this.currentPos % this.path.length];
76
};
77

78
/**
79
* @return {string}
80
*/
81
Robot.prototype.getDir = function () {
82
return this.currentPos === -1
83
? DIRECTIONS[0] // we haven't moved
84
: DIRECTIONS[this.dirAtSquare[this.currentPos % this.dirAtSquare.length]];
85
};
86

87
/**
88
* Your Robot object will be instantiated and called as such:
89
* var obj = new Robot(width, height)
90
* obj.step(num)
91
* var param_2 = obj.getPos()
92
* var param_3 = obj.getDir()
93
*/

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0