-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpart-one.js
59 lines (47 loc) · 1.21 KB
/
part-one.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
const { input } = require('./input');
const { InfiniteGrid } = require('./infinite-grid');
let grid = new InfiniteGrid({
load: input,
parseAs: Number,
});
function doFlashing(flashed) {
let new_flashes = [];
for (let id of flashed) {
let neighbors = grid.neighbors(...InfiniteGrid.toCoords(id), true);
for (let { coord, value } of neighbors.values()) {
let [x, y] = coord;
let new_value = value + 1;
// Only flash once when we are at 10 energy
if (new_value === 10) {
new_flashes.push(InfiniteGrid.toId(x, y));
}
grid.set(x, y, new_value);
}
}
return new_flashes;
}
let count = 0;
for (let i = 0; i < 100; i++) {
let flashed = [];
// Initially increment all octo's energy by `1` and flash those that are greater than 9
for (let [id, value] of grid) {
let new_value = value + 1;
grid.grid.set(id, new_value);
if (new_value === 10) {
flashed.push(id);
}
}
count += flashed.length;
// Keep flashing until we stop creating more flashes
while (flashed.length > 0) {
flashed = doFlashing(flashed);
count += flashed.length;
}
// Reset all flashed octos back to `0`
for (let [id, value] of grid) {
if (value > 9) {
grid.grid.set(id, 0);
}
}
}
console.log(count);