-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpart-two.js
108 lines (91 loc) · 2.63 KB
/
part-two.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const { uniq } = require('lodash');
const { input } = require('./input');
const { Hex } = require('./hex-grid-red-blob');
const { createGrid } = require('./hex');
const GRID = createGrid(input);
const DIAGONALS = Object.keys(Hex.diagonals);
const getGridColor = (coord) => (GRID[coord] === 1 ? 1 : 0);
function pruneZeroes() {
let white = Object.entries(GRID).filter(([c, v]) => v === 0);
for (let [c] of white) {
delete GRID[c];
}
}
// Memoized
const parseCoordStr = (() => {
const cache = {};
return (str) => {
if (!cache[str]) {
cache[str] = str.split(',').map((v) => parseInt(v, 10));
}
return cache[str];
};
})();
const getHexFromCoordStr = (() => {
const cache = {};
return (coord) => {
if (!cache[coord]) {
cache[coord] = new Hex(...parseCoordStr(coord));
}
return cache[coord];
};
})();
function getNeighborsOfCoordStr(coord, color) {
let cell = getHexFromCoordStr(coord);
let neighbors = [];
for (let dir of DIAGONALS) {
let neighbor = cell.add(Hex.diagonals[dir]);
let neighbor_str = neighbor.toString();
if (color !== undefined) {
let neighbor_color = getGridColor(neighbor_str);
if (neighbor_color !== color) {
continue;
}
}
neighbors.push(neighbor_str);
}
return neighbors;
}
function getUniqueNeighborsOfCoordsStr(coords, color) {
return uniq(coords.map((coord) => getNeighborsOfCoordStr(coord, color)).flat());
}
function getNeighborColorCount(coord, color) {
let count = 0;
let cell = getHexFromCoordStr(coord);
for (let dir of DIAGONALS) {
let neighbor = cell.add(Hex.diagonals[dir]);
let neighbor_str = neighbor.toString();
let neighbor_color = getGridColor(neighbor_str);
if (neighbor_color === color) {
count++;
}
}
return count;
}
for (let iterations = 0; iterations < 100; iterations++) {
// Keeps memory low, and prevents us from iterating over white tiles
pruneZeroes();
let tiles = Object.keys(GRID);
let black_tiles_to_flip = [];
for (let tile of tiles) {
let black_neighboar_tiles_count = getNeighborColorCount(tile, 1);
if (black_neighboar_tiles_count === 0 || black_neighboar_tiles_count > 2) {
black_tiles_to_flip.push(tile);
}
}
let white_neighbor_tiles = getUniqueNeighborsOfCoordsStr(tiles, 0);
let white_tiles_to_flip = [];
for (let white_neighbor_tile of white_neighbor_tiles) {
let black_neighbor_tiles_count = getNeighborColorCount(white_neighbor_tile, 1);
if (black_neighbor_tiles_count === 2) {
white_tiles_to_flip.push(white_neighbor_tile);
}
}
for (let tile of black_tiles_to_flip) {
GRID[tile] = 0;
}
for (let tile of white_tiles_to_flip) {
GRID[tile] = 1;
}
}
console.log(Object.values(GRID).reduce((a, b) => a + b, 0));