AoC 2021 D25: Sea Cucumber

TypeScript | Problem statement | Source code | Tags: Cellular automata

← Previous Back to AoC Index

I thought this requires something clever, but simulating it straightforwardly is fast enough. For example, this is the code for moving the east-facing cucumbers:

function moveEast(mat: string[][]) {
const newMat = mat.map((line) => line.slice());
for (let row = 0; row < mat.length; row++) {
for (let col = 0; col < mat[0].length; col++) {
if (
mat[row][col] === ">" &&
mat[row][(col + 1) % mat[0].length] === "."
) {
newMat[row][(col + 1) % mat[0].length] = ">";
newMat[row][col] = ".";
}
}
}
return newMat;
}
ts

Checking for stabilization is done with a tiny trick: JSON.stringify(mat) === JSON.stringify(newMat2).

← Previous Back to AoC Index