diff --git a/04/code.js b/04/code.js new file mode 100644 index 0000000..13ce4e4 --- /dev/null +++ b/04/code.js @@ -0,0 +1,143 @@ +import { readFileSync } from 'node:fs'; + +let sampleMode = false; +let usedArray = []; + +const sampleArray = readFileSync('sample.txt').toString().split("\n"); +const inputArray = readFileSync('input.txt').toString().split("\n"); + +if (sampleMode) { + usedArray = [].concat(sampleArray); +} else { + usedArray = [].concat(inputArray); +} + +// Part One + +console.time("part1"); +let sum = 0; + +for (let i = 0; i < usedArray.length; i++) { + let row = usedArray[i]; + for (let j = 0; j < row.length; j++) { + let cell = row[j]; + if (cell === '@') { + let openSides = 0; + const left = j > 0 ? row[j - 1] : '.'; + const right = j < row.length - 1 ? row[j + 1] : '.'; + const up = i > 0 ? usedArray[i - 1][j] : '.'; + const down = i < usedArray.length - 1 ? usedArray[i + 1][j] : '.'; + const upperLeft = i > 0 && j > 0 ? usedArray[i - 1][j - 1] : '.'; + const upperRight = i > 0 && j < row.length - 1 ? usedArray[i - 1][j + 1] : '.'; + const lowerLeft = i < usedArray.length - 1 && j > 0 ? usedArray[i + 1][j - 1] : '.'; + const lowerRight = i < usedArray.length - 1 && j < row.length - 1 ? usedArray[i + 1][j + 1] : '.'; + if (left === '.') { + openSides++; + } + if (right === '.') { + openSides++; + } + if (up === '.') { + openSides++; + } + if (down === '.') { + openSides++; + } + if (upperLeft === '.') { + openSides++; + } + if (upperRight === '.') { + openSides++; + } + if (lowerLeft === '.') { + openSides++; + } + if (lowerRight === '.') { + openSides++; + } + if (openSides >= 5) { + sum++; + } + } + } +} + +console.timeEnd("part1"); +console.log(sum); + + +// Part Two + +let usedArray2 = []; +if (sampleMode) { + usedArray2 = [].concat(sampleArray); +} else { + usedArray2 = [].concat(inputArray); +} + +console.time("part2"); +let sum2 = 0; +let done = false; + +while (!done) { + getOpenSides(sum2, usedArray2); +} + +console.timeEnd("part2"); +console.log(sum2); + +// functions + +function getOpenSides(startingSum, usedArray) { + const newSum = JSON.parse(JSON.stringify(startingSum)); + for (let i = 0; i < usedArray.length; i++) { + let row = [...usedArray[i].split('')]; + for (let j = 0; j < row.length; j++) { + let cell = row[j]; + if (cell === '@') { + let openSides = 0; + const left = j > 0 ? row[j - 1] : '.'; + const right = j < row.length - 1 ? row[j + 1] : '.'; + const up = i > 0 ? usedArray[i - 1][j] : '.'; + const down = i < usedArray.length - 1 ? usedArray[i + 1][j] : '.'; + const upperLeft = i > 0 && j > 0 ? usedArray[i - 1][j - 1] : '.'; + const upperRight = i > 0 && j < row.length - 1 ? usedArray[i - 1][j + 1] : '.'; + const lowerLeft = i < usedArray.length - 1 && j > 0 ? usedArray[i + 1][j - 1] : '.'; + const lowerRight = i < usedArray.length - 1 && j < row.length - 1 ? usedArray[i + 1][j + 1] : '.'; + if (left === '.') { + openSides++; + } + if (right === '.') { + openSides++; + } + if (up === '.') { + openSides++; + } + if (down === '.') { + openSides++; + } + if (upperLeft === '.') { + openSides++; + } + if (upperRight === '.') { + openSides++; + } + if (lowerLeft === '.') { + openSides++; + } + if (lowerRight === '.') { + openSides++; + } + if (openSides >= 5) { + sum2++; + row[j] = '.'; + } + } + } + usedArray[i] = row.join(''); + } + + if (sum2 === newSum) { + done = true; + } +} \ No newline at end of file