-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem17a.js
72 lines (67 loc) · 1.9 KB
/
problem17a.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
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}`] = 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 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]));
console.log(minX, maxX, minY, maxY, minZ, maxZ);
// Allow for growth at the edge
for (z = minZ - 1; (z <= maxZ + 1); z++) {
for (y = minY - 1; (y <= maxY + 1); y++) {
for (x = minX - 1; (x <= maxX + 1); x++) {
consider(x, y, z);
}
}
}
function consider(x, y, z) {
let neighbors = 0;
const live = cubes[`${x}:${y}:${z}`];
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))) {
if (cubes[`${nx}:${ny}:${nz}`]) {
neighbors++;
}
}
}
}
}
if (live) {
if ((neighbors === 2) || (neighbors === 3)) {
newCubes[`${x}:${y}:${z}`] = true;
}
} else if (neighbors === 3) {
newCubes[`${x}:${y}:${z}`] = true;
}
}
console.log(newCubes);
cubes = newCubes;
}
console.log(cubes);
console.log(Object.keys(cubes).length);