diff options
Diffstat (limited to '2022/18-Boiling-Boulders/first.js')
-rw-r--r-- | 2022/18-Boiling-Boulders/first.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/2022/18-Boiling-Boulders/first.js b/2022/18-Boiling-Boulders/first.js new file mode 100644 index 0000000..7cd43d0 --- /dev/null +++ b/2022/18-Boiling-Boulders/first.js @@ -0,0 +1,38 @@ +import * as fs from "fs"; + +function load(filename) { + return fs.readFileSync(filename, "utf8") + .trim() + .split("\n"); +} + +let example = load("example"); +let input = load("input"); + +function countSides(cube, input) { + let [x, y, z] = cube.split(",").map(n => parseInt(n)); + let count = 6; + if (input.includes([x+1, y, z].join(","))) count--; + if (input.includes([x-1, y, z].join(","))) count--; + if (input.includes([x, y+1, z].join(","))) count--; + if (input.includes([x, y-1, z].join(","))) count--; + if (input.includes([x, y, z+1].join(","))) count--; + if (input.includes([x, y, z-1].join(","))) count--; + return count; +} + +function solve(input) { + let count = 0; + input.forEach(cube => { + count += countSides(cube, input); + }); + return count; +} + +const exampleOutput = solve(example); +if (exampleOutput !== 64) { + console.log("Example failed with " + exampleOutput); + process.exit(1); // eslint-disable-line +} + +console.log(solve(input)); |