조이스틱

Happhee·2022년 1월 28일
0

[ Lv2 ] programmers

목록 보기
2/32
post-thumbnail

📝 조이스틱

🖥 나의 JS 코드

function calNumber ( result_number, cursor){
    console.log(cursor);
    console.log(result_number);
    let left = cursor - 0;
    
    let cal_number = new Array(result_number.length);
    for(let i = 0 ; i < result_number.length ; i++){
        cal_number[i] = result_number[i] < 13 ? result_number[i] 
        : 26 - result_number[i]
        
        if(result_number[i] != 0){
            
            let gap ;
                if(Math.abs(left- i)  <  (result_number.length -Math.abs(i-cursor)) ){
                   gap =  Math.abs(left-i) 
                   } 
                else {
                   gap =  (result_number.length -Math.abs(i-cursor))

                }
              console.log(i + ' : ' + Math.abs(left- i) + '/ '+ (result_number.length -Math.abs(i-cursor))+ 
                         ' : ' + gap);
        
            cal_number[i] += gap;

        } else {
            cal_number[i] = 1000;
        }
    }
    console.log(cal_number);
    console.log('-------------');
    
    return cal_number;
}

function solution(name) {
    let index = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let result_number = [];
    index = index.split('');
    name.split('').map((alpha)=> {
       result_number.push(index.indexOf(alpha))
        
    })

    let result = new Array();
    
    function re(result_number, current_number,sum){
        if(Math.min(...current_number) === 1000){
            result.push(sum);
            return;
        } else{
            let min = Math.min(...current_number);
            let min_arr = [];
            for(let i =0 ; i < current_number.length ; i++){
                if(min === current_number[i])
                    min_arr.push(i);
            }
            sum += min;
            
            for(let i = 0 ; i < min_arr.length ; i++){
                let new_result = [...result_number];
                new_result[min_arr[i]] = 0;
                let new_current= calNumber (new_result, min_arr[i]);
                re(new_result, new_current, sum);
            }  
        }
    }
    let current_number = calNumber(result_number, 0)
    let init = current_number[0];
    current_number[0] = 0;
    // re(result_number,current_number,init );
    re(result_number,calNumber(result_number, 0),0 );
    console.log(result);
    
    return Math.min(...result);
}
profile
즐기면서 정확하게 나아가는 웹프론트엔드 개발자 https://happhee-dev.tistory.com/ 로 이전하였습니다

0개의 댓글