-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
47 lines (42 loc) · 1.22 KB
/
index.js
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
const { expect } = require('chai');
/**
* @param {string} instructions
* @return {boolean}
*/
let isRobotBounded = function(instructions) {
const init = [0, 0];
let dire = [0, 1];
const turn = (pre, direction) => {
if (0 === pre[0] && 1 === pre[1]) {
if ('L' === direction) return [-1, 0];
else return [1, 0];
} else if (0 === pre[0] && -1 === pre[1]) {
if ('L' === direction) return [1, 0];
else return [-1, 0];
} else if (-1 === pre[0] && 0 === pre[1]) {
if ('L' === direction) return [0, -1];
else return [0, 1];
} else {
if ('L' === direction) return [0, 1];
else return [0, -1];
}
};
for (let i = 0; i < instructions.length; i++) {
if (instructions[i] !== 'G') dire = turn(dire, instructions[i]);
else {
init[0] += dire[0];
init[1] += dire[1];
}
}
if (0 === init[0] && 0 === init[1]) {
return true;
}
return !(0 === dire[0] && 1 === dire[1]);
};
it('robot-bounded-in-circle', () => {
expect(isRobotBounded('GGLLGG')).to.be.true;
expect(isRobotBounded('GG')).to.be.false;
expect(isRobotBounded('GL')).to.be.true;
expect(isRobotBounded('LLGRL')).to.be.true;
expect(isRobotBounded('LRRRRLLLRL')).to.be.true;
});