Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- js 거리두기 확인하기
- 자바스크립트 스코프
- 백준 1339번 자바스크립트
- 옵셔널체이닝
- emotion RSC
- next13 emotion
- TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more:
- 백준 2108 자바스크립트
- suspense 병목현상
- 프로그래머스 문자열 압축
- js 스코프
- 프로그래머스 거리두기 확인하기
- 스코프
- 구름톤 챌린지
- 백준 1339번 js
- 백준 2108 nodejs
- js 문자열 압축
- 백준 1339번 nodejs
- 카카오 코테
- js
- 구름톤 챌린지 회고
- 구름톤
- suspense 비동기
- suspense 동작원리
- 사용성 개선
- suspense react-query
- 리액트쿼리 suspense
- 자바스크립트 문자열 압축
- app router emotion
- emtion app router
Archives
- Today
- Total
Lennon FE
[Javascript] 구름톤 챌린지 2주차 5문제 풀기 본문
728x90
반응형
[내가 만든 프로젝트 참여하기 프로젝트]
6. 문자열 나누기
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on('line', (line) => {
input.push(line);
if (input.length === 2) {
rl.close();
}
});
rl.on('close', () => {
const [N, S] = input;
const { divideArr, sortedDivideStrArr } = divideStringIntoThreeParts(N, S);
const answer = getMaximumValue(divideArr, sortedDivideStrArr);
console.log(answer);
});
const divideStringIntoThreeParts = (N, str) => {
const divideArr = [];
const set = new Set();
for (let i = 1; i < N - 1; i++) {
for (let j = i + 1; j < N; j++) {
const [first, second, third] = [
str.substring(0, i),
str.substring(i, j),
str.substring(j),
];
set.add(first);
set.add(second);
set.add(third);
divideArr.push([first, second, third]);
}
}
return { divideArr, sortedDivideStrArr: [...set].sort() };
}
const getMaximumValue = (divideArr, sortedDivideStrArr) => {
const values = divideArr.map((arr) => {
const sum = arr.reduce(
(prev, cur) => prev + sortedDivideStrArr.indexOf(cur) + 1,
0
);
return sum;
});
return Math.max(...values);
};
7. 구름 찾기 깃발
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on('line', (line) => {
input.push(line);
if (input.length === +input[0].split(' ')[0] + 1) {
rl.close();
}
});
rl.on('close', () => {
const [NK, ...matrix] = input;
const [N, K] = NK.split(' ').map(Number);
const matrixArr = matrix.map((v) => v.split(' ').map(Number));
const answer = matrixArr.reduce((accRow, row, i) => {
const rowAnswer = row.reduce((accCol, cell, j) => {
if (cell === 1) {
return accCol;
}
const goormCount = goormCounter(matrixArr, i, j);
if (goormCount === K) {
return accCol + 1;
}
return accCol;
}, accRow);
return rowAnswer;
}, 0);
console.log(answer);
});
const goormCounter = (matrixArr, i, j) => {
const coordinates = [
[i - 1, j - 1],
[i, j - 1],
[i + 1, j - 1],
[i + 1, j],
[i + 1, j + 1],
[i, j + 1],
[i - 1, j + 1],
[i - 1, j],
];
// 좌표에서 0보다 크고, matrixArr.length보단 작은 걸 모두 만족하는것만 filter
const filterCoordinates = coordinates.filter((coordinate) =>
coordinate.every((v) => v >= 0 && v < matrixArr.length)
);
return filterCoordinates.reduce(
(prev, cur) => prev + matrixArr[cur[0]][cur[1]],
0
);
};
8. 통증
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const [bandage, medicine, painkiller] = [1, 7, 14];
let input;
rl.on('line', (line) => {
input = +line;
rl.close();
});
rl.on('close', () => {
const answer = getAnswer(input);
console.log(answer);
});
const getAnswer = (num) => {
let count = 0;
while (num !== 0) {
if (num >= painkiller) {
count++;
num -= painkiller;
continue;
}
if (num >= medicine) {
count++;
num -= medicine;
continue;
}
count++;
num -= bandage;
}
return count;
};
9. 폭탄 구현하기
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on('line', (line) => {
input.push(line);
if (input.length === input[0].split(' ').map(Number) + 1) {
rl.close();
}
});
rl.on('close', () => {
const [N, K] = input.shift().split(' ').map(Number);
let grounds = input
.slice(0, N)
.map((v) => v.split(' ').map((cell) => (isNaN(+cell) ? cell : +cell)));
const coordinates = input.slice(N).map((v) => v.split(' ').map((v) => v - 1));
const atCoordinatesSet = new Set();
// Set에 @ 좌표 저장
grounds.forEach((row, index) => {
row.forEach((cell, i) => {
if (cell === '@') {
atCoordinatesSet.add(`${index},${i}`);
}
});
});
// 좌표를 순회하며 grounds 최신화
coordinates.forEach((coordinate) => {
const [x, y] = coordinate;
grounds = boomResult(grounds, atCoordinatesSet, x, y);
});
// 2X2 배열 직렬화
const flatMatrix = grounds.flatMap((row) => row);
// 숫자만 filter
const filterNumberMatrix = flatMatrix.filter(Number);
// 최고값 추출 (기본값 0)
const answer = Math.max(...filterNumberMatrix, 0);
console.log(answer);
});
const boomResult = (matrix, atCoordinatesSet, x, y) => {
const updateMatrix = [...matrix];
const coordinates = [
[x, y],
[x, y + 1],
[x + 1, y],
[x, y - 1],
[x - 1, y],
];
// 유효하지 않은 좌표 필터링
const filterCoordinates = coordinates.filter((coordinate) => {
const [x, y] = coordinate;
return x >= 0 && x < matrix.length && y >= 0 && y < matrix[0].length;
});
for (const [x, y] of filterCoordinates) {
const currentValue = updateMatrix[x][y];
if (currentValue === '#') {
continue;
}
if (currentValue === '@' || atCoordinatesSet.has([x, y].join(','))) {
updateMatrix[x][y] = currentValue === '@' ? 2 : currentValue + 2;
} else {
updateMatrix[x][y] = currentValue + 1;
}
}
return updateMatrix;
};
10. GameJam
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on('line', (line) => {
input.push(line);
if (input.length === +input[0] + 3) {
rl.close();
}
});
rl.on('close', () => {
const [N, goormee, player, ...matrix] = input;
const goormeeStartCoordinate = goormee.split(' ').map((v) => +v - 1);
const playerStartCoordinate = player.split(' ').map((v) => +v - 1);
const board = matrix.map((v) => v.split(' '));
const goormeeScore = getAnswer(+N, goormeeStartCoordinate, board);
const playerScore = getAnswer(+N, playerStartCoordinate, board);
if (goormeeScore > playerScore) {
console.log(`goorm ${goormeeScore}`);
} else {
console.log(`player ${playerScore}`);
}
});
const updateCoordinate = ({ N, currentCoordinate, direction, board }) => {
let [updateY, updateX] = currentCoordinate;
if (direction === 'R' && updateX + 1 >= N) {
updateX = 0;
} else if (direction === 'L' && updateX - 1 < 0) {
updateX = N - 1;
} else if (direction === 'R') {
updateX += 1;
} else if (direction === 'L') {
updateX -= 1;
}
if (direction === 'D' && updateY + 1 >= N) {
updateY = 0;
} else if (direction === 'U' && updateY - 1 < 0) {
updateY = N - 1;
} else if (direction === 'D') {
updateY += 1;
} else if (direction === 'U') {
updateY -= 1;
}
return [updateY, updateX];
};
const getAnswer = (N, coordinate, board) => {
let score = 0;
let currentCoordinate = [...coordinate];
const passedCoordinates = [coordinate];
let isPassed = 0;
while (!isPassed) {
const direction = board[currentCoordinate[0]][currentCoordinate[1]].at(-1);
const value = board[currentCoordinate[0]][currentCoordinate[1]].replace(
direction,
''
);
for (let i = 0; i < +value; i++) {
score++;
currentCoordinate = updateCoordinate({
N,
currentCoordinate,
direction,
board,
});
isPassed = passedCoordinates.filter(
(coor) =>
coor[0] === currentCoordinate[0] && coor[1] === currentCoordinate[1]
).length;
passedCoordinates.push(currentCoordinate);
if (isPassed) {
break;
}
}
}
return score;
};
728x90
반응형
'🔥 Algorithm' 카테고리의 다른 글
[Javascript] 구름톤 챌린지 1주차 5문제 풀기 (0) | 2023.08.16 |
---|
Comments