2022-18 in js
This commit is contained in:
parent
c14d3aac12
commit
ece85a2e99
7 changed files with 2927 additions and 0 deletions
38
2022/18-Boiling-Boulders/first.js
Normal file
38
2022/18-Boiling-Boulders/first.js
Normal file
|
@ -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));
|
Loading…
Add table
Add a link
Reference in a new issue