-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
49 lines (40 loc) · 1.27 KB
/
main.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
const util = require('util');
const fs = require("fs");
const path = process.argv[1].replace("/main.js", "");
const buffer = fs.readFileSync(`${path}/test.txt`);
const input = buffer.toString().trimEnd().split("\n");
let state = [];
input.forEach((row, y) => {
row.split("").forEach((char, x) => {
state.push({x: x, y: y, z: 0, active: char === "#"});
});
});
let expansion = state => {
let newCubes = [];
//-1, 0, 1 etc
return newCubes;
};
let shouldStayActive = cube => {
//exactly 2 or 3 of its neighbors are also active, the cube remains active.
return cube.active;
};
let shouldTurnActive = cube => {
//exactly 3 of its neighbors are active, the cube becomes active.
return cube.active;
};
let cycle = lastState => {
let nextState = [...lastState, ...expansion(lastState)];
nextState.forEach(cube => {
cube.active = ((cube.active && shouldStayActive(cube)) ||
(!cube.active && shouldTurnActive(cube)));
});
return nextState;
};
console.log("Initial state", state);
for (let i = 0; i < 6; i++) {
state = cycle(state);
}
console.log("6 cycles state", state);
let activeCubes = state.reduce((a, b) => a + (b.active ? 1 : 0), 0);
console.log(`Actives after 6 cycles: ${activeCubes} (part 1)`);
// console.log(`Another something: ${2} (part 2)`);