From b5a407d3d9e3bac2b1b7ed7c8dedaee74b32dd7c Mon Sep 17 00:00:00 2001 From: hwinkr Date: Thu, 16 Nov 2023 19:00:20 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[=EC=B5=9C=ED=98=84=EC=9B=85]=203=EC=A3=BC?= =?UTF-8?q?=EC=B0=A8=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\227\260\352\265\254\354\206\214.js" | 105 ++++++++++++++++++ ...4\354\246\210 \353\270\224\353\241\235.js" | 86 ++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 "chw/3week/\354\227\260\352\265\254\354\206\214.js" create mode 100644 "chw/3week/\355\224\204\353\240\214\354\246\210 \353\270\224\353\241\235.js" diff --git "a/chw/3week/\354\227\260\352\265\254\354\206\214.js" "b/chw/3week/\354\227\260\352\265\254\354\206\214.js" new file mode 100644 index 0000000..4331a2b --- /dev/null +++ "b/chw/3week/\354\227\260\352\265\254\354\206\214.js" @@ -0,0 +1,105 @@ +const readline = require("readline"); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +const input = []; + +rl.on("line", (line) => { + input.push(line); +}).on("close", function () { + const [row, col] = input + .shift() + .split(" ") + .map((el) => Number(el)); + + const graph = input.map((line) => line.split(" ").map((num) => Number(num))); + const answer = solution(graph, row, col); + console.log(answer); + process.exit(); +}); + +const VIRUS = 2; +const WALL = 1; +const PATH = 0; +const DIRESCTIONS = [ + [-1, 0], + [0, 1], + [1, 0], + [0, -1], +]; + +function findTargetPositions(graph, target) { + const targetPositions = graph.reduce((accVirus, row, i) => { + const virusInRow = row + .map((cell, j) => cell === target && [i, j]) + .filter((value) => !!value); + return [...accVirus, ...virusInRow]; + }, []); + + return targetPositions; +} + +const countSafezone = (graph) => { + const safezone = graph.reduce((accSafezone, row, i) => { + const safezoneInrow = row + .map((cell, j) => cell === PATH && [i, j]) + .filter((value) => !!value).length; + return accSafezone + safezoneInrow; + }, 0); + + return safezone; +}; + +const isPositionsInLab = (nx, ny, row, col) => { + return nx >= 0 && nx < row && ny >= 0 && ny < col; +}; + +const polluteLab = (graph, row, col, virus) => { + while (virus.length > 0) { + const [x, y] = virus.shift(); + DIRESCTIONS.forEach((dir) => { + const [dx, dy] = dir; + const nx = x + dx; + const ny = y + dy; + + if (isPositionsInLab(nx, ny, row, col) && graph[nx][ny] === PATH) { + graph[nx][ny] = VIRUS; + virus.push([nx, ny]); + } + }); + } +}; + +const solution = (graph, row, col) => { + const virusPositions = findTargetPositions(graph, VIRUS); + const pathPositions = findTargetPositions(graph, PATH); + + let answer = -Infinity; + + for (let i = 0; i < pathPositions.length - 2; i += 1) { + for (let j = i + 1; j < pathPositions.length - 1; j += 1) { + for (let k = j + 1; k < pathPositions.length; k += 1) { + const copyGraph = graph.map((row) => [...row]); + const addWallPositions = [ + pathPositions[i], + pathPositions[j], + pathPositions[k], + ]; + + addWallPositions.forEach((addWall) => { + const [x, y] = addWall; + copyGraph[x][y] = WALL; + }); + + polluteLab(copyGraph, row, col, [...virusPositions]); + const safezone = countSafezone(copyGraph); + answer = Math.max(answer, safezone); + } + } + } + + return answer; +}; diff --git "a/chw/3week/\355\224\204\353\240\214\354\246\210 \353\270\224\353\241\235.js" "b/chw/3week/\355\224\204\353\240\214\354\246\210 \353\270\224\353\241\235.js" new file mode 100644 index 0000000..f3c76aa --- /dev/null +++ "b/chw/3week/\355\224\204\353\240\214\354\246\210 \353\270\224\353\241\235.js" @@ -0,0 +1,86 @@ +const DIRECTIONS = [ + [0, 1], + [1, 1], + [1, 0], +]; + +const DESTROYED_MARK = "-"; + +const getUniqueArray = (arr) => { + return [...new Set(arr.join("|").split("|"))].map((v) => + v.split(",").map((v) => Number(v)) + ); +}; + +const countDestroyedBlocks = (board) => { + const destoryedBlocks = board.reduce((acc, row, i) => { + const destoryedBlocksInRow = row + .map((col, j) => col === DESTROYED_MARK && [i, j]) + .filter((value) => !!value); + + return acc + destoryedBlocksInRow.length; + }, 0); + + return destoryedBlocks; +}; + +const changePositions = (row, col, board, positions) => { + const changeBlocks = []; + for (let j = 0; j < col; j++) { + for (let i = row - 1; i >= 0; i -= 1) { + if (positions.some((pos) => i === pos[0] && j === pos[1])) { + continue; + } + changeBlocks.push(board[i][j]); + } + + while (changeBlocks.length < row) { + changeBlocks.push(DESTROYED_MARK); + } + + for (let i = 0; i < row; i += 1) { + board[i][j] = changeBlocks.pop(); + } + } +}; + +const updatePositions = (positions, board, targetPosition) => { + const [x, y] = targetPosition; + + if ( + DIRECTIONS.every((dir) => { + const [dx, dy] = dir; + return board[x + dx] && board[x + dx][y + dy] === board[x][y]; + }) + ) { + const destoryedPosition = [ + ...DIRECTIONS.map((dir) => [x + dir[0], y + dir[1]]), + [x, y], + ]; + positions.push(...destoryedPosition); + } +}; + +function solution(row, col, board) { + const gameBoard = board.map((row) => row.split("")); + while (true) { + const positions = []; + gameBoard.forEach((row, i) => { + row.forEach((col, j) => { + if (col === DESTROYED_MARK) return; + updatePositions(positions, gameBoard, [i, j]); + }); + }); + + if (!positions.length) { + return countDestroyedBlocks(gameBoard); + } + + const uniquePositions = getUniqueArray(positions); + changePositions(row, col, gameBoard, uniquePositions); + } +} + +console.log( + solution(6, 6, ["TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"]) +); From 7b91c0e27856a097ad749a08b11aa0da470e4c5e Mon Sep 17 00:00:00 2001 From: hwinkr Date: Fri, 17 Nov 2023 11:40:22 +0900 Subject: [PATCH 2/2] =?UTF-8?q?chore(change=20variable=20name):=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=EB=AA=85,=20=ED=95=A8=EC=88=98=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\246\210 \353\270\224\353\241\235.js" | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git "a/chw/3week/\355\224\204\353\240\214\354\246\210 \353\270\224\353\241\235.js" "b/chw/3week/\355\224\204\353\240\214\354\246\210 \353\270\224\353\241\235.js" index f3c76aa..ac661a2 100644 --- "a/chw/3week/\355\224\204\353\240\214\354\246\210 \353\270\224\353\241\235.js" +++ "b/chw/3week/\355\224\204\353\240\214\354\246\210 \353\270\224\353\241\235.js" @@ -6,26 +6,27 @@ const DIRECTIONS = [ const DESTROYED_MARK = "-"; -const getUniqueArray = (arr) => { +const getUniquePositions = (arr) => { return [...new Set(arr.join("|").split("|"))].map((v) => v.split(",").map((v) => Number(v)) ); }; -const countDestroyedBlocks = (board) => { - const destoryedBlocks = board.reduce((acc, row, i) => { - const destoryedBlocksInRow = row +const countDestructedBlocks = (board) => { + const destructedBlocks = board.reduce((acc, row, i) => { + const destructedBlocksInRow = row .map((col, j) => col === DESTROYED_MARK && [i, j]) .filter((value) => !!value); - return acc + destoryedBlocksInRow.length; + return acc + destructedBlocksInRow.length; }, 0); - return destoryedBlocks; + return destructedBlocks; }; const changePositions = (row, col, board, positions) => { const changeBlocks = []; + for (let j = 0; j < col; j++) { for (let i = row - 1; i >= 0; i -= 1) { if (positions.some((pos) => i === pos[0] && j === pos[1])) { @@ -44,7 +45,7 @@ const changePositions = (row, col, board, positions) => { } }; -const updatePositions = (positions, board, targetPosition) => { +const checkDestructibleBlocks = (positions, board, targetPosition) => { const [x, y] = targetPosition; if ( @@ -53,34 +54,31 @@ const updatePositions = (positions, board, targetPosition) => { return board[x + dx] && board[x + dx][y + dy] === board[x][y]; }) ) { - const destoryedPosition = [ + const destoryedPositions = [ ...DIRECTIONS.map((dir) => [x + dir[0], y + dir[1]]), [x, y], ]; - positions.push(...destoryedPosition); + positions.push(...destoryedPositions); } }; function solution(row, col, board) { const gameBoard = board.map((row) => row.split("")); + while (true) { const positions = []; gameBoard.forEach((row, i) => { row.forEach((col, j) => { if (col === DESTROYED_MARK) return; - updatePositions(positions, gameBoard, [i, j]); + checkDestructibleBlocks(positions, gameBoard, [i, j]); }); }); if (!positions.length) { - return countDestroyedBlocks(gameBoard); + return countDestructedBlocks(gameBoard); } - const uniquePositions = getUniqueArray(positions); + const uniquePositions = getUniquePositions(positions); changePositions(row, col, gameBoard, uniquePositions); } } - -console.log( - solution(6, 6, ["TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"]) -);