Skip to content

Instantly share code, notes, and snippets.

@Friss
Created December 17, 2020 15:53
Show Gist options
  • Save Friss/71104a05dbf186248bac54df22580bb0 to your computer and use it in GitHub Desktop.
Save Friss/71104a05dbf186248bac54df22580bb0 to your computer and use it in GitHub Desktop.
const fs = require('fs').promises;
(async () => {
console.log(`Reading input from ${__dirname}/${process.argv[2]}.txt`);
const inputData = await fs.readFile(`${__dirname}/${process.argv[2]}.txt`);
const inputs = inputData
.toString()
.split('\n')
.filter((n) => n);
let grid = new Map();
inputs.forEach((line, x) => {
line.split('').forEach((item, y) => {
grid.set(`[${x}, ${y}, 0]`, item);
});
});
let cycle = 0;
let xBounds = [0, inputs.length];
let yBounds = [0, inputs.length];
let zBounds = [0, 1];
while (cycle < 6) {
const nextGrid = new Map();
xBounds = [xBounds[0] - 1, xBounds[1] + 1];
yBounds = [yBounds[0] - 1, yBounds[1] + 1];
zBounds = [zBounds[0] - 1, zBounds[1] + 1];
for (let x = xBounds[0]; x < xBounds[1]; x++) {
for (let y = yBounds[0]; y < yBounds[1]; y++) {
for (let z = zBounds[0]; z < zBounds[1]; z++) {
let numActive = 0;
for (const dX of [-1, 0, 1]) {
for (const dY of [-1, 0, 1]) {
for (const dZ of [-1, 0, 1]) {
if (dX === 0 && dY === 0 && dZ === 0) {
continue;
}
if (grid.get(`[${x + dX}, ${y + dY}, ${z + dZ}]`) === '#') {
numActive++;
}
}
}
}
const value = grid.get(`[${x}, ${y}, ${z}]`);
if (numActive === 3) {
nextGrid.set(`[${x}, ${y}, ${z}]`, '#');
} else if (value === '#' && numActive === 2) {
nextGrid.set(`[${x}, ${y}, ${z}]`, '#');
}
}
}
}
grid = nextGrid;
cycle++;
}
console.log('part1', [...grid.values()].filter((i) => i === '#').length);
let grid2 = new Map();
inputs.forEach((line, x) => {
line.split('').forEach((item, y) => {
grid2.set(`[${x}, ${y}, 0, 0]`, item);
});
});
let cycle2 = 0;
let xBounds2 = [0, inputs.length];
let yBounds2 = [0, inputs.length];
let zBounds2 = [0, 1];
let hyperBounds2 = [0, 1];
while (cycle2 < 6) {
const nextGrid = new Map();
xBounds2 = [xBounds2[0] - 1, xBounds2[1] + 1];
yBounds2 = [yBounds2[0] - 1, yBounds2[1] + 1];
zBounds2 = [zBounds2[0] - 1, zBounds2[1] + 1];
hyperBounds2 = [hyperBounds2[0] - 1, hyperBounds2[1] + 1];
for (let x = xBounds2[0]; x < xBounds2[1]; x++) {
for (let y = yBounds2[0]; y < yBounds2[1]; y++) {
for (let z = zBounds2[0]; z < zBounds2[1]; z++) {
for (let h = hyperBounds2[0]; h < hyperBounds2[1]; h++) {
let numActive = 0;
for (const dX of [-1, 0, 1]) {
for (const dY of [-1, 0, 1]) {
for (const dZ of [-1, 0, 1]) {
for (const dH of [-1, 0, 1]) {
if (dX === 0 && dY === 0 && dZ === 0 && dH === 0) {
continue;
}
if (
grid2.get(
`[${x + dX}, ${y + dY}, ${z + dZ}, ${h + dH}]`
) === '#'
) {
numActive++;
}
}
}
}
}
const value = grid2.get(`[${x}, ${y}, ${z}, ${h}]`);
if (numActive === 3) {
nextGrid.set(`[${x}, ${y}, ${z}, ${h}]`, '#');
} else if (value === '#' && numActive === 2) {
nextGrid.set(`[${x}, ${y}, ${z}, ${h}]`, '#');
}
}
}
}
}
grid2 = nextGrid;
cycle2++;
}
console.log('part2', [...grid2.values()].filter((i) => i === '#').length);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment