-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem17b.js
78 lines (73 loc) · 2.23 KB
/
problem17b.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const fs = require('fs');
const data = fs.readFileSync('input17.txt', 'utf8');
// const data = `
// .#.
// ..#
// ###
// `;
const lines = data.split('\n').map(line => line.trim()).filter(line => line.length);
let cubes = {};
let y = 0;
for (const line of lines) {
let x = 0;
for (const char of line.split('')) {
if (char === '#') {
cubes[`${x}:${y}:${0}:${0}`] = true;
}
x++;
}
y++;
}
console.log(cubes);
for (let i = 0; (i < 6); i++) {
const newCubes = {};
const considered = {};
const coords = Object.keys(cubes).map(key => key.split(':'));
const minX = Math.min(...coords.map(coords => coords[0]));
const minY = Math.min(...coords.map(coords => coords[1]));
const minZ = Math.min(...coords.map(coords => coords[2]));
const minT = Math.min(...coords.map(coords => coords[3]));
const maxX = Math.max(...coords.map(coords => coords[0]));
const maxY = Math.max(...coords.map(coords => coords[1]));
const maxZ = Math.max(...coords.map(coords => coords[2]));
const maxT = Math.max(...coords.map(coords => coords[3]));
console.log(minX, maxX, minY, maxY, minZ, maxZ, minT, maxT);
// Allow for growth at the edge
for (let t = minT - 1; (t <= maxT + 1); t++) {
for (let z = minZ - 1; (z <= maxZ + 1); z++) {
for (let y = minY - 1; (y <= maxY + 1); y++) {
for (let x = minX - 1; (x <= maxX + 1); x++) {
consider(x, y, z, t);
}
}
}
}
function consider(x, y, z, t) {
let neighbors = 0;
const live = cubes[`${x}:${y}:${z}:${t}`];
for (let nt = t - 1; (nt <= t + 1); nt++) {
for (let nz = z - 1; (nz <= z + 1); nz++) {
for (let ny = y - 1; (ny <= y + 1); ny++) {
for (let nx = x - 1; (nx <= x + 1); nx++) {
if (!((nx === x) && (ny === y) && (nz === z) && (nt === t))) {
if (cubes[`${nx}:${ny}:${nz}:${nt}`]) {
neighbors++;
}
}
}
}
}
}
if (live) {
if ((neighbors === 2) || (neighbors === 3)) {
newCubes[`${x}:${y}:${z}:${t}`] = true;
}
} else if (neighbors === 3) {
newCubes[`${x}:${y}:${z}:${t}`] = true;
}
}
console.log(newCubes);
cubes = newCubes;
}
console.log(cubes);
console.log(Object.keys(cubes).length);