-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.js
58 lines (49 loc) · 1.27 KB
/
solution.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
48
49
50
51
52
53
54
55
56
57
58
function solution(numbers, hand) {
const STAR = 10;
const HASH = 12;
let left = STAR;
let right = HASH;
let result = '';
const click = (clickHand, number) => {
if (clickHand === 'L') {
left = number;
} else {
right = number;
}
result += clickHand;
};
const getDistance = (a, b) => {
const getRow = (number) => Math.floor((number - 1) / 3);
const getCol = (number) => (number - 1) % 3;
const rowDistance = Math.abs(getRow(a) - getRow(b));
const colDistance = Math.abs(getCol(a) - getCol(b));
return rowDistance + colDistance;
};
numbers.forEach((number) => {
const target = number !== 0 ? number : 11;
switch (target % 3) {
case 1:
click('L', target);
break;
case 0:
click('R', target);
break;
case 2: {
const leftDistance = getDistance(left, target);
const rightDistance = getDistance(right, target);
if (leftDistance === rightDistance) {
if (hand === 'left') {
click('L', target);
} else {
click('R', target);
}
} else if (leftDistance < rightDistance) {
click('L', target);
} else {
click('R', target);
}
}
}
});
return result;
}