Skip to content

Commit

Permalink
2021.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Doruk Gurleyen committed Dec 11, 2021
1 parent 5a4dd3c commit 77dceae
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
63 changes: 63 additions & 0 deletions challenges/2021/11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Part 1
const solvePuzzle1 = (input) => {
input = parseInput(input);
let flashed = 0;

for (let i = 0; i < 100; i++) {
const flashedCoords = new Set();
for (let y = 0; y < input.length; y++) {
for (let x = 0; x < input[0].length; x++) {
flashed += increment(y, x, input, flashedCoords);
}
}
}

return flashed;
};

// Part 2
const solvePuzzle2 = (input) => {
input = parseInput(input);
const [width, height] = [input.length, input[0].length];
const boardSize = width * height;
let [step, flashedCoords] = [0, new Set()];

while (flashedCoords.size < boardSize) {
flashedCoords = new Set();
step += 1;
for (let y = 0; y < width; y++) {
for (let x = 0; x < height; x++) {
increment(y, x, input, flashedCoords);
}
}
}

return step;
};

const increment = (y, x, board, flashedCoords) => {
let flashed = 0;
if (board[y] === undefined || board[y][x] === undefined || flashedCoords.has(`${y},${x}`)) return flashed;

if (board[y][x] >= 0 && board[y][x] < 9) {
board[y][x]++;
} else {
flashedCoords.add(`${y},${x}`);
flashed = 1;
board[y][x] = 0;
forEachSurrounding(y, x, (h, w) => (flashed += increment(h, w, board, flashedCoords)));
}
return flashed;
};

const parseInput = (input) => input.split("\n").map((l) => l.split("").map((x) => Number(x)));
const forEachSurrounding = (y, x, callback) => {
for (let h = y - 1; h <= y + 1; h++) {
for (let w = x - 1; w <= x + 1; w++) {
if (h !== y || w !== x) callback(h, w);
}
}
};

require(__dirname + "/../../utils/test.js").test(__filename, __dirname, solvePuzzle1);
require(__dirname + "/../../utils/test.js").test(__filename, __dirname, solvePuzzle2);
10 changes: 10 additions & 0 deletions challenges/2021/inputs/11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
5212166716
1567322581
2268461548
3481561744
6248342248
6526667368
5627335775
8124511754
4614137683
4724561156

0 comments on commit 77dceae

Please sign in to comment.