728x90
https://softeer.ai/practice/6255
Softeer - ํ๋์๋์ฐจ๊ทธ๋ฃน SW์ธ์ฌํ๋ณดํ๋ซํผ
softeer.ai
์ํํฐ์ด ๋ ๋ฒจ 3 ..!
์ํํฐ์ด๋ ์ง์ง ์ฝ์ง ์๋ค ใ .ใ ๊ทธ๋๋ ์ด ๋ฌธ์ ๋ ์ค๋ช ์ด ์์ฐจ์ ์ด๊ณ ์์ธํด์ ์ข์์
1์๊ฐ๋์ ๋นก์ง์คํ๋๋ฐ ํ๋ฒ์ ๋ง์ ๋ฐ์๋ค ๐ต
์๋ฌด๋๋ ์์ง js๊ฐ ์ต์ํ์ง ์๋ค๋ณด๋ ์ฉ๊ตฌํ์ผ๋ก ์ฝ๋๊ฐ ๊ธธ์ด์ง๋ฏํ๋ฐ,
๋ค๋ฅธ ๋ถ์ ํ์ด๋ฅผ ๋ณด๋ ๋ ์งง๋๋ผ
charCodeAt ๋ฐฐ์๊ฐ๋๋ค
๊ทธ์ ์กด๊ฒฝ์ค๋ฌ์ธ ๋ฐ๋ฆ.,,
๋์ ํ์ด
const fs = require('fs');
const input = fs.readFileSync(0,'utf-8').trim().split('\n');
let alphas = ["A","B","C",'D','E','F','G','H','I','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z']; // J๋ ์ ์ธ
const msg = input[0].split('');
const key = input[1].split('');
const makeMap=(text)=>{
let map = [];
let tmparr = [];
let txt_ = new Set(text); // ์ค๋ณต์ ๊ฑฐ
let txt = [...txt_] // ๋ฐฐ์ด๋ก ์ฌ๋ณํ
txt.map((t)=>{
let a = alphas.indexOf(t);
if(a>=0){
alphas.splice(a,1); // ๋ฉ์์ง์ ์กด์ฌํ๋ ์ํ๋ฒณ ์ ๊ฑฐ
}
})
for(let i=0; i<txt.length; ++i){ // ๋ฉ์์ง ์ํ๋ฒณ ์ฑ์ฐ๊ธฐ
tmparr.push(txt[i]);
if(tmparr.length>=5) {
map.push(tmparr);
tmparr = [];
}
}
for(let i=0; i<alphas.length; ++i){ // ๋ฉ์์ง์ ์๋ ์ํ๋ฒณ ์ฑ์ฐ๊ธฐ
tmparr.push(alphas[i]);
if(tmparr.length>=5) {
map.push(tmparr);
tmparr = [];
}
}
return map;
}
const makePair=(text)=>{
let txt = [];
let tmp = "";
while(1){
if(text.length<=1) break;
if(text[0]===text[1]) {
if(text[0]==='X') text.splice(1,0,'Q');
else text.splice(1,0,'X');
}
let a = text.shift();
let b = text.shift();
tmp = a+b;
txt.push(tmp);
tmp = "";
}
if(text.length === 1) {
tmp = text[0]+'X';
txt.push(tmp);
}
return txt;
}
const makePassword=(pairs, map)=>{
let newMap = [];
let answer = "";
for(let i=0; i<5; ++i){
let tmparr = [];
for(let j=0; j<5; ++j){
tmparr.push(map[j][i]);
}
newMap.push(tmparr);
}
// console.log(newMap);
pairs.map((pair)=>{
let newPair = "";
// 1. pair[0], pair[1] ๊ฐ์ ํ์ธ์ง ํ์ธ
let isChanged = false;
for(let i=0; i<5; ++i){
let first = map[i].indexOf(pair[0]);
let second = map[i].indexOf(pair[1]);
if(first>=0 && second>=0){
// console.log(pair[0],pair[1],`${i+1}ํ`,first, second);
if(first === 4) first = -1;
if(second === 4) second = -1;
newPair = map[i][first+1] + map[i][second+1];
// console.log(newPair);
answer += newPair;
isChanged = true;
break;
}
}
// 2. ๊ฐ์ ์ด์ธ์ง ํ์ธ
if(!isChanged){
for(let i=0; i<5; ++i){
let first = newMap[i].indexOf(pair[0]);
let second = newMap[i].indexOf(pair[1]);
if(first>=0 && second>=0){
// console.log(pair[0],pair[1],`${i+1}์ด`,first, second);
if(first === 4) first = -1;
if(second === 4) second = -1;
newPair = newMap[i][first+1] + newMap[i][second+1];
// console.log(newPair);
answer += newPair;
isChanged = true;
break;
}
}
}
// 3. ์ปฌ๋ผ์ด ์๋ก ๊ตํ๋ ์์น๋ก
if(!isChanged){
let firstRow = 0; let firstCol = 0;
let secondRow = 0; let secondCol = 0;
for(let i=0; i<5; ++i){
for(let j=0; j<5; ++j){
if(map[i][j] === pair[0]) {
firstRow = i;
firstCol = j;
}
if(map[i][j] === pair[1]) {
secondRow = i;
secondCol = j;
}
}
}
newPair = map[firstRow][secondCol] + map[secondRow][firstCol];
answer += newPair;
isChanged = true;
// console.log(newPair)
}
})
return answer;
}
let map = makeMap(key);
let pairs = makePair(msg);
// console.log(pairs);
console.log(makePassword(pairs, map));
JS๋ ์ตํ์๋ก ๋ฌธ์์ด ๋ค๋ฃจ๋ ๋ง์ด ์๋ ๊ฒ ๊ฐ๋ค. (split, splice, slice, shift ...)
ํจ์๊ฐ ๋ค์ํด์ ์๊ทผ ์ฌ๋ฏธ์๋ค!!
728x90