교육/코테

[프로그래머스]JS 키패드 누르기

가이버2 2022. 5. 18. 21:32

https://programmers.co.kr/learn/courses/30/lessons/67256

 

코딩테스트 연습 - 키패드 누르기

[1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] "right" "LRLLLRLLRRL" [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] "left" "LRLLRRLLLRR" [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] "right" "LLRLLRLLRL"

programmers.co.kr

function solution(numbers, hand) {

    function calDistance(dist, now, hand){
        let iDistance = 0;
        let distPad = [numPad[dist][0], numPad[dist][1]];
        let currentPad = [numPad[now][0], numPad[now][1]];
        while(currentPad[0] != distPad[0] ||
                currentPad[1] != distPad[1]
            ){
            iDistance++;
            // 좌표 우 좌
            if(hand == "L")
                if(currentPad[0] < distPad[0]) {currentPad[0]++; continue;}
            if(hand == "R")
                if(currentPad[0] > distPad[0]) {currentPad[0]--; continue;}

            // 좌표 위 아래
            if( currentPad[1] < distPad[1]) { currentPad[1]++; continue;}
            if( currentPad[1] > distPad[1]) { currentPad[1]--; continue;}
        }

        return iDistance;
    }

    const numPad = {
        1: [0,0], 2:[1,0], 3:[2,0],
        4: [0,1], 5:[1,1], 6:[2,1],
        7: [0,2], 8:[1,2], 9:[2,2],
        "*": [0,3], 0:[1,3], "#":[2,3]
    }

    //손위치 초기화
    let Now = {
        R : '#',
        L : '*'
    }
    // 손잡이
    let handed = hand == "right" ? "R" : "L";

    //솔루션
    let result = "";

    for(let i=0; i<numbers.length; i++){
        if(numbers[i]%3 == 1){ //왼쪽 나머지 1
            result += 'L';
            Now.L = numbers[i];
            continue;
        }
    
        if(numbers[i]%3 == 0 && numbers[i] != 0){ //오른쪽 나머지 0
            result += 'R';
            Now.R = numbers[i];
            continue;
        }
    
        if(numbers[i]%3 == 2 | numbers[i] == 0){ //거리 측정후 누르기
            //거리 계산후
            let leftDistance = calDistance(numbers[i], Now.L, "L");
            let rightDistance = calDistance(numbers[i], Now.R, "R");
            if(leftDistance == rightDistance) {
                result += handed;
                Now[handed] = numbers[i];
            }else{
                let moveHand = leftDistance < rightDistance ? "L" : "R"; 
                result += moveHand;
                Now[moveHand] = numbers[i];
            }

            continue;
        }
    }
 
    return result;
}

다른 사람 풀이

function solution(numbers, hand) {
  hand = hand[0] === "r" ? "R" : "L"
  let position = [1, 4, 4, 4, 3, 3, 3, 2, 2, 2]
  let h = { L: [1, 1], R: [1, 1] }
  return numbers.map(x => {
    if (/[147]/.test(x)) {
      h.L = [position[x], 1]
      return "L"
    }
    if (/[369]/.test(x)) {
      h.R = [position[x], 1]
      return "R"
    }
    let distL = Math.abs(position[x] - h.L[0]) + h.L[1]
    let distR = Math.abs(position[x] - h.R[0]) + h.R[1]
    if (distL === distR) {
      h[hand] = [position[x], 0]
      return hand
    }
    if (distL < distR) {
      h.L = [position[x], 0]
      return "L"
    }
    h.R = [position[x], 0]
    return "R"
  }).join("")
}