Day 18 was easy

This commit is contained in:
John Shaver 2022-12-19 02:34:34 -08:00
parent 15ebc5c200
commit b09f37bb7c
3 changed files with 2283 additions and 0 deletions

54
2022/18/index.js Normal file
View file

@ -0,0 +1,54 @@
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;
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)]) {
surfaceCount++
}
}
}
console.log("Answer: ", surfaceCount);
}
main();

93
2022/18/index2.js Normal file
View file

@ -0,0 +1,93 @@
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();

2136
2022/18/input.txt Normal file

File diff suppressed because it is too large Load diff