aboutsummaryrefslogtreecommitdiff
path: root/2022/18-Boiling-Boulders/first.js
blob: 7cd43d0b8c721ffa0206dc6f0a37003456ddca82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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));