-
Notifications
You must be signed in to change notification settings - Fork 0
/
키패드누르기.js
64 lines (55 loc) · 1.36 KB
/
키패드누르기.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
59
60
61
62
63
64
function calcDistance(numberPos, currentPos) {
const distance =
Math.abs(numberPos[0] - currentPos[0]) +
Math.abs(numberPos[1] - currentPos[1]);
return distance;
}
function solution(numbers, hand) {
const result = [];
const keypad = [
[3, 6, 9, '#'],
[2, 5, 8, 0],
[1, 4, 7, '*'],
];
let rightPos = [0, 3];
let leftPos = [2, 3];
function move(side, numberPos) {
if (side == 'L') {
leftPos = numberPos;
} else {
rightPos = numberPos;
}
result.push(side);
}
for (const number of numbers) {
let numberPos = [0, 0];
for (let x = 0; x < keypad.length; x++) {
for (let y = 0; y < keypad[0].length; y++) {
if (keypad[x][y] === number) {
numberPos = [x, y];
break;
}
}
}
if (numberPos[0] === 0) {
move('R', numberPos);
} else if (numberPos[0] === 2) {
move('L', numberPos);
} else {
const rightDistance = calcDistance(numberPos, rightPos);
const leftDistance = calcDistance(numberPos, leftPos);
if (leftDistance < rightDistance) {
move('L', numberPos);
} else if (leftDistance > rightDistance) {
move('R', numberPos);
} else {
if (hand === 'left') {
move('L', numberPos);
} else {
move('R', numberPos);
}
}
}
}
return result.join('');
}