Skip to content

Commit 3fe5827

Browse files
committed
day 11
1 parent 9006e18 commit 3fe5827

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Diff for: day11/day11.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Advent of Code: Day 11 TypeScript
2+
const relative_positions = [[0, -1], [0, 1], [-1, -1], [-1, 0], [-1, 1], [1, -1], [1, 0], [1, 1]];
3+
4+
function foo(data) {
5+
const map = data.map(line => line.split('').map(x => parseInt(x)));
6+
let c = 0;
7+
for (let i = 0; i < 100; i++) c += step(map);
8+
return c;
9+
}
10+
11+
function step(arr) {
12+
const flashes = inc(arr);
13+
const flashesCount = flash(flashes, arr);
14+
reset(arr);
15+
return flashesCount;
16+
}
17+
18+
function inc(arr) {
19+
const flashes = [];
20+
for (let i = 0; i < arr.length; i++) {
21+
for (let j = 0; j < arr[i].length; j++) {
22+
arr[i][j]++;
23+
if (arr[i][j] > 9) flashes.push([i, j]);
24+
}
25+
}
26+
return flashes;
27+
}
28+
29+
function flash(flashes, arr) {
30+
let c = 0;
31+
while (flashes.length > 0) {
32+
const[y, x] = flashes.pop()!;
33+
if (arr[y][x] !== 10) continue;
34+
c++;
35+
for (const [dy, dx] of relative_positions) {
36+
if (arr[y + dy] === undefined || arr[y + dy][x + dx] === undefined || arr[y + dy][x + dx] >= 10) continue;
37+
arr[y + dy][x + dx]++;
38+
if (arr[y + dy][x + dx] === 10) flashes.push([y + dy, x + dx]);
39+
}
40+
}
41+
return c;
42+
}
43+
44+
function reset(arr) {
45+
for (let row = 0; row < arr.length; row++)
46+
for (let col = 0; col < arr[row].length; col++)
47+
if ((arr[row][col] as number) > 9)
48+
arr[row][col] = 0;
49+
}
50+
51+
console.log("Part 1 Result: " + foo(require("fs").readFileSync("input.txt", "utf-8").split("\n")));

0 commit comments

Comments
 (0)