function energised(mirrors, start = { r: 0, c: 0, dir: [0, 1] }) {
const grid = AOC.gMap(() => 0, mirrors);
const [nRows, nCols] = [grid.length, grid[0].length];
const history = new Set();
const beams = [start];
grid[start.r][start.c] = 1;
while (beams.length > 0) {
const b = beams.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 mirror = mirrors[b.r][b.c];
b.dir = rotate(b.dir, mirror);
if (mirror === "-" && b.dir[0] !== 0) {
beams.push({ r: b.r, c: b.c, dir: [0, -1] });
b.dir = [0, 1];
} else if (mirror === "|" && b.dir[1] !== 0) {
beams.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;
beams.push({ r: r, c: c, dir: b.dir });
}
}
}
return AOC.sum(grid.flat());
}