function energised(tiles, start = { r: 0, c: 0, dir: [0, 1] }) {
const grid = AOC.gMap(() => 0, tiles);
const [nRows, nCols] = [grid.length, grid[0].length];
const history = new Set();
const photons = [start];
grid[start.r][start.c] = 1;
while (photons.length > 0) {
const b = photons.shift();
const hash =
b.r * nCols * 9 + b.c * 9 + (b.dir[0] + 1) + (b.dir[1] + 1) * 3;
if (!history.has(hash)) {
history.add(hash);
const tile = tiles[b.r][b.c];
b.dir = rotate(b.dir, tile);
if (tile === "-" && b.dir[0] !== 0) {
photons.push({ r: b.r, c: b.c, dir: [0, -1] });
b.dir = [0, 1];
} else if (tile === "|" && b.dir[1] !== 0) {
photons.push({ r: b.r, c: b.c, dir: [1, 0] });
b.dir = [-1, 0];
}
const [r, c] = [b.r + b.dir[0], b.c + b.dir[1]];
if (r >= 0 && c >= 0 && r < nRows && c < nCols) {
grid[r][c] = 1;
photons.push({ r: r, c: c, dir: b.dir });
}
}
}
return AOC.sum(grid.flat());
}