93 lines
2.2 KiB
JavaScript
93 lines
2.2 KiB
JavaScript
const fs = require('fs/promises');
|
|
|
|
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
|
|
const arrayOfLength = n => Array.from(Array(n).keys()).map(() => {});
|
|
//minus
|
|
const keypress = async () => {
|
|
process.stdin.setRawMode(true)
|
|
return new Promise(resolve => process.stdin.once('data', data => {
|
|
const byteArray = [...data]
|
|
if (byteArray.length > 0 && byteArray[0] === 3) {
|
|
console.log('^C')
|
|
process.exit(1)
|
|
}
|
|
process.stdin.setRawMode(false)
|
|
resolve()
|
|
}))
|
|
}
|
|
|
|
const coords = ([x, y, z]) => `${x},${y},${z}`;
|
|
|
|
|
|
|
|
const main = async () => {
|
|
const input = (await fs.readFile("./input.txt", 'utf8')).slice(0, -1).split("\n");
|
|
const scan = input.reduce((s, point) => {
|
|
s[point] = true;
|
|
return s;
|
|
}, {});
|
|
|
|
|
|
const points = input.map(p => p.split(",").map(x => parseInt(x)));
|
|
|
|
let surfaceCount = 0;
|
|
|
|
const maxValues = points.reduce(([mx, my, mz], [x, y, z]) => {
|
|
return [Math.max(mx,x), Math.max(my, y), Math.max(mz,z)];
|
|
}, [-Infinity, -Infinity, -Infinity]);
|
|
|
|
|
|
const minValues = points.reduce(([mx, my, mz], [x, y, z]) => {
|
|
return [Math.min(mx,x), Math.min(my, y), Math.min(mz,z)];
|
|
}, [Infinity, Infinity, Infinity]);
|
|
|
|
const lookForExit = (point, visited = {}) => {
|
|
const [x,y,z] = point;
|
|
if(x > maxValues[0] || x < minValues[0]
|
|
|| y > maxValues[1] || y < minValues[1]
|
|
|| z > maxValues[2] || z < minValues[2]) {
|
|
return true;
|
|
}
|
|
visited[coords(point)] = true;
|
|
|
|
const possibleNeighbors = [
|
|
[0, 0, 1],
|
|
[0, 0, -1],
|
|
[0, 1, 0],
|
|
[0, -1, 0],
|
|
[1, 0, 0],
|
|
[-1, 0, 0]
|
|
].map(([dx, dy, dz]) => ([x + dx, y + dy, z + dz]));
|
|
for(const n of possibleNeighbors) {
|
|
if(!visited[coords(n)] && !scan[coords(n)]) {
|
|
if(lookForExit(n, visited)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
for(const [x, y, z] of points) {
|
|
const possibleNeighbors = [
|
|
[0, 0, 1],
|
|
[0, 0, -1],
|
|
[0, 1, 0],
|
|
[0, -1, 0],
|
|
[1, 0, 0],
|
|
[-1, 0, 0]
|
|
].map(([dx, dy, dz]) => ([x + dx, y + dy, z + dz]));
|
|
|
|
for(const n of possibleNeighbors) {
|
|
if(!scan[coords(n)]) {
|
|
if(lookForExit(n)) {
|
|
surfaceCount++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log("Answer: ", surfaceCount);
|
|
|
|
}
|
|
|
|
main();
|