ah-ryeong / Ahgorithm

0 stars 0 forks source link

[JavaScript] 비밀지도 #12

Open ah-ryeong opened 1 year ago

ah-ryeong commented 1 year ago

문제

네오는 평소 프로도가 비상금을 숨겨놓는 장소를 알려줄 비밀지도를 손에 넣었다. 그런데 이 비밀지도는 숫자로 암호화되어 있어 위치를 확인하기 위해서는 암호를 해독해야 한다. 다행히 지도 암호를 해독할 방법을 적어놓은 메모도 함께 발견했다.

function solution(n, arr1, arr2) {
    var answer = [];

    arr1 = arr1.map(function(itm) {
     let box = itm.toString(2);
     if(box.length > n) {
       box = box.substr(0,n);
     } else if(box.length < n) {
       box = box.padStart(n, "0");
     }
     box = box.replace(/1/g, '#').replace(/0/g, ' ');
     return box;
    });

    arr2 = arr2.map(function(itm) {
     let box = itm.toString(2);
     if(box.length > n) {
       box = box.substr(0,n);
     } else if(box.length < n) {
       box = box.padStart(n, "0");
     }
     box = box.replace(/1/g, '#').replace(/0/g, ' ');
     return box;
    });

    for(let i = 0; i < arr1.length; i++) {
      let str = "";
      for(let j = 0; j < n ; j++) {
        if(arr1[i][j] === " " && arr2[i][j] === " ") {
          str = str+" ";
        } else {
          str = str+"#"
        }
      }
      answer.push(str);
    }
    return answer;
}