반응형
정답 풀이
const path = process.platform === "linux" ? "/dev/stdin" : "input.txt"; // 리눅스로 테스트할 땐 따로 설정해주어야 합니다.
let input = require("fs")
.readFileSync(path)
.toString()
.trim()
.split("\n")
.map(i => i.split(' ').map(Number));
/* input 정리 */
const [N, M, R] = input.shift();
const calculators = input.pop();
/* 문제 풀이 */
const go = (...args) => args.reduce((a, f) => f(a));
for (const calculator of calculators) {
switch (calculator) {
case 1: upDownReverse(); break;
case 2: leftRightReverse(); break;
case 3: right90degreeRotate(); break;
case 4: left90degreeRotate(); break;
case 5: divideFourSectionAndRotateRight(); break;
case 6: divideFourSectionAndRotateLeft(); break;
}
}
input.forEach(i => console.log(i.join(' ')));
// 1
function upDownReverse() {
input = [...input].reverse();
}
// 2
function leftRightReverse() {
input = input.map(row => [...row].reverse());
}
// 3
function right90degreeRotate() {
const n = input.length;
const m = input[0].length;
const newArray = Array.from({ length: m }, () => Array.from({ n }));
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
newArray[i][j] = input[n - j - 1][i];
}
}
input = newArray
}
// 4
function left90degreeRotate() {
return go(
input,
right90degreeRotate,
right90degreeRotate,
right90degreeRotate,
);
}
// 5
function divideFourSectionAndRotateRight() {
const n = input.length / 2;
const m = input[0].length / 2;
const top = input.slice(0, n);
const bottom = input.slice(n);
const part1 = top.map(row => row.slice(0, m));
const part2 = top.map(row => row.slice(m));
const part3 = bottom.map(row => row.slice(0, m));
const part4 = bottom.map(row => row.slice(m));
input = [
...part3.map((row, index) => [...row, ...part1[index]]),
...part4.map((row, index) => [...row, ...part2[index]]),
];
}
// 6
function divideFourSectionAndRotateLeft() {
return go(
input,
divideFourSectionAndRotateRight,
divideFourSectionAndRotateRight,
divideFourSectionAndRotateRight,
);
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 20438번: 출석체크 (JavaScript, NodeJS) (0) | 2022.06.15 |
---|---|
[백준] 15486번: 퇴사2 (JavaScript, NodeJS) (0) | 2022.02.13 |
[백준] 1106번: 호텔 (JavaScript, NodeJS) (0) | 2022.02.12 |
[백준] 9465번: 스티커 (JavaScript, NodeJS) (0) | 2022.02.11 |
[백준] 1890번: 점프 (JavaScript, NodeJS) (0) | 2022.02.10 |