Skip to content

Instantly share code, notes, and snippets.

@cgreening
Created December 17, 2020 09:25
Show Gist options
  • Select an option

  • Save cgreening/3757cd9f10f28ad393132543cc1e5b41 to your computer and use it in GitHub Desktop.

Select an option

Save cgreening/3757cd9f10f28ad393132543cc1e5b41 to your computer and use it in GitHub Desktop.
import { X_OK } from "constants";
import { isAccessor } from "typescript";
class CubeSpace {
activeCount = 0;
potentiallyActive: {
[key: number]: { x: number; y: number; z: number; w: number };
} = {};
memory = new Set<number>();
getKey(x: number, y: number, z: number, w: number) {
// big assumption here that we don't go out of range!!!!
return x * 100 * 100 * 100 + y * 100 * 100 + z * 100 + w;
}
getValue(x: number, y: number, z: number, w: number): boolean {
const key = this.getKey(x, y, z, w);
return this.memory.has(key);
}
setValue(x: number, y: number, z: number, w: number, value: boolean) {
const key = this.getKey(x, y, z, w);
this.activeCount++;
this.memory.add(key);
// update the potentially active list
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
for (let dz = -1; dz <= 1; dz++) {
for (let dw = -1; dw <= 1; dw++) {
this.potentiallyActive[
this.getKey(x + dx, y + dy, z + dz, w + dw)
] = {
x: x + dx,
y: y + dy,
z: z + dz,
w: w + dw,
};
}
}
}
}
}
}
function day17Part1(input: string) {
function simulate(cube: CubeSpace): CubeSpace {
const result = new CubeSpace();
Object.values(cube.potentiallyActive).forEach((coords) => {
const { x, y, z, w } = coords;
let activeNeighbours = 0;
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
for (let dz = -1; dz <= 1; dz++) {
for (let dw = -1; dw <= 1; dw++) {
if (dx == 0 && dy == 0 && dz == 0 && dw == 0) {
continue;
}
if (cube.getValue(x + dx, y + dy, z + dz, w + dw)) {
activeNeighbours++;
}
}
}
}
}
if (cube.getValue(x, y, z, w)) {
if (activeNeighbours === 2 || activeNeighbours === 3) {
result.setValue(x, y, z, w, true);
}
} else {
if (activeNeighbours === 3) {
result.setValue(x, y, z, w, true);
}
}
});
return result;
}
let cube = new CubeSpace();
input.split("\n").forEach((yRow, y) => {
yRow.split("").forEach((isActive, x) => {
cube.setValue(x, y, 0, 0, isActive === "#");
});
});
console.time("day17Part2");
for (let i = 0; i < 6; i++) {
cube = simulate(cube);
}
console.timeEnd("day17Part2");
return cube.activeCount;
}
console.log(
day17Part1(
`##......
.##...#.
.#######
..###.##
.#.###..
..#.####
##.####.
##..#.##`
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment