-
Notifications
You must be signed in to change notification settings - Fork 4
/
day11.js
74 lines (65 loc) · 1.8 KB
/
day11.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
const fs = require("fs");
const matrix = fs
.readFileSync("day11.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.split("\n") // Split on newline
.filter(Boolean) // Remove empty lines
.map((line) => [...line].map(Number));
let firstSynchronizedFlash;
let flashes = 0;
let turnCounter = 0;
function increaseEnergy({ i, j, explodedSet }) {
// Check that it's a valid coordinate
if (typeof matrix[i] === "undefined") return;
if (typeof matrix[i][j] === "undefined") return;
// Check it hasn't exploded yet
const key = i + ":" + j;
if (explodedSet.has(key)) {
return;
}
// Act
matrix[i][j]++;
if (matrix[i][j] > 9) {
// Explode!!!
matrix[i][j] = 0;
explodedSet.add(key);
flashes++;
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
if (x === 0 && y === 0) continue;
increaseEnergy({ i: i + x, j: j + y, explodedSet });
}
}
return;
}
}
function turn() {
turnCounter++;
let explodedSet = new Set();
// increase each spot by 1
// if value> 9 then explode (on all 8 neighbors) / can only explode once per turn
// once exploded, reset to 0
for (let i = 0; i < matrix.length; i++) {
const line = matrix[i];
for (let j = 0; j < line.length; j++) {
increaseEnergy({ i, j, explodedSet });
}
}
// console.table(matrix);
if (
typeof firstSynchronizedFlash === "undefined" &&
explodedSet.size === matrix.length * matrix[0].length
) {
firstSynchronizedFlash = turnCounter;
}
}
for (let i = 0; i < 100; i++) {
turn();
}
console.log(flashes);
if (typeof firstSynchronizedFlash === "undefined") {
while (typeof firstSynchronizedFlash === "undefined") {
turn();
}
}
console.log(firstSynchronizedFlash);