개인용 복습공간

프로그래머스 - 키패드 누르기 본문

programmers

프로그래머스 - 키패드 누르기

taehwanis 2023. 10. 12. 17:05
*과 #을 숫자값으로 두고 누르기
function solution(numbers, hand) {
    let lHand = '10' , rHand = '12';
    
    return numbers.reduce((acc, el) => {
        if(el == 0) {
            el = 11;  
        };
        
        const temp = el % 3;
        
        if(temp == 0){
            rHand = el;
            acc += 'R';
        }else if(temp == 1){
            lHand = el;
            acc += 'L';
        }else{        
            const r_position = Math.abs(Math.ceil(el / 3) - Math.ceil(rHand / 3)) + (rHand % 3 == temp ? 0 : 1);
            const l_position = Math.abs(Math.ceil(el / 3) - Math.ceil(lHand / 3)) + (lHand % 3 == temp ? 0 : 1);

           if(r_position > l_position){
               lHand = el;
               acc += 'L';
           }else if (r_position < l_position){
               rHand = el;
               acc += 'R';
           }else if (r_position == l_position){
               if(hand == 'right'){
                    rHand = el;
                    acc += 'R';
               }else{
                    lHand = el;
                    acc += 'L';
               }
           }
        }
        
        return acc;
    },  [])
}

 

Comments