1차 비밀지도
문제분석
지도 한 변의 길이를 나타내는 n, 지도 1과 2를 나타내는 배열 arr1, arr2가 매개변수로 주어졌을 때, 지도를 합쳐 비밀지도 암호를 해독하는 함수 solution 만들기
문제조건
1. 1<=n<=16
2. arr1, arr2는 길이가 n이고 원소는 정수이다
3. 정수 배열의 각 원소 x를 이진수로 변환했을 때의 길이는 n 이하
풀이
function solution(n, arr1, arr2) {
const result = [];
for( let i = 0 ; i<n; i++) {
let temp = sumel(n,arr1[i],arr2[i]);
temp = temp.replace(/[1-2]/g,"#").replace(/[0]/g," ");
result.push(temp);
}
return result;
}
function sumel(n,el1,el2){
const f = (el) => parseInt(el.toString(2),10);
let sumtoStr = f(el1)+f(el2)+"";
if(sumtoStr.length<n){
sumtoStr = sumtoStr.padStart(n,0);
}
return sumtoStr;
}
다른 풀이
function solution(n, arr1, arr2) {
return arr1.map((v, i) => addZero(n, (v | arr2[i]).toString(2)).replace(/1|0/g, a => +a ? '#' : ' '));
}
const addZero = (n, s) => {
return '0'.repeat(n - s.length) + s;
}