Skip to content

Instantly share code, notes, and snippets.

@bluepichu
Created December 11, 2021 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluepichu/ef312fb97e86d2797637347be0d9e3b6 to your computer and use it in GitHub Desktop.
Save bluepichu/ef312fb97e86d2797637347be0d9e3b6 to your computer and use it in GitHub Desktop.
import { List } from "immutable";
import { Advent, GridDirections, int } from "advent";
const { compute, computeCheck } = await Advent({ day: 11 });
compute(async (input) => {
// const data = input.tokens(/,/).map(int);
// const data = input.ints();
let ans = 0;
const lines = input.lines();
let grid = lines.map((line) => line.split("").map(int));
for (let i = 1; i < 1000000; i++) {
let out = step(grid);
if (out === grid.length * grid[0].length) {
return i;
}
}
return ans;
}, 2);
function step(grid: number[][]) {
let count = 0;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
grid[i][j]++;
}
}
while (true) {
let updated = false;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] >= 10) {
grid[i][j] = -1000;
updated = true;
count++;
for (let [di, dj] of GridDirections) {
if (i + di >= 0 && i + di < grid.length && j + dj >= 0 && j + dj < grid[i].length) {
grid[i + di][j + dj]++;
}
}
}
}
}
if (!updated) {
break;
}
}
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] < 0) {
grid[i][j] = 0;
}
}
}
return count;
}
// computeCheck(async function* (input) {
// yield 0;
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment