diff --git a/Day11/Octopus.ts b/Day11/Octopus.ts new file mode 100644 index 0000000..341a745 --- /dev/null +++ b/Day11/Octopus.ts @@ -0,0 +1,127 @@ +import { readLines } from "https://deno.land/std@0.177.1/io/read_lines.ts"; + +async function Part1(generations: number) { + const file = await Deno.open("./Day11/input.txt"); + const lines = readLines(file); + + let flashes = 0; + const levels: number[][] = []; + const neighbours = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]] + + for await (const line of lines) { + levels.push([]); + const initialLevels = line.split(""); + for (const n of initialLevels) { + levels[levels.length - 1].push(+n); + } + } + + for (let g = 0; g < generations; g++) { + const coords: number[][] = [] + for (const i in levels) { + for (const j in levels[i]) { + coords.push([+i,+j]); + } + } + + while (coords.length != 0) { + const [i,j]: number[] = coords.pop()!; + if (levels[i][j] >= 9) { + flashes++; + levels[i][j] = -1; + for (const [a,b] of neighbours) { + if (!(0 <= i + a && i + a <= levels.length - 1 && 0 <= j + b && j + b <= levels[i].length - 1)) { + continue + } + if (levels[i + a][j + b] != -1) { + levels[i + a][j + b]++; + if (levels[i + a][j + b] > 9) { + coords.push([i + a, j + b]); + } + } + } + } else { + if (levels[i][j] != -1) { + levels[i][j]++; + } + } + } + + for (const i in levels) { + for (const j in levels[i]) { + if (levels[i][j] == -1) { + levels[i][j] = 0; + } + } + } + + } + return flashes +} + +async function Part2() { + const file = await Deno.open("./Day11/input.txt"); + const lines = readLines(file); + + let lastFlashes = 0; + let generation = 0; + const levels: number[][] = []; + const neighbours = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]] + + for await (const line of lines) { + levels.push([]); + const initialLevels = line.split(""); + for (const n of initialLevels) { + levels[levels.length - 1].push(+n); + } + } + + while (lastFlashes != levels.length * levels[0].length) { + lastFlashes = 0; + generation++; + + const coords: number[][] = [] + for (const i in levels) { + for (const j in levels[i]) { + coords.push([+i,+j]); + } + } + + while (coords.length != 0) { + const [i,j]: number[] = coords.pop()!; + if (levels[i][j] >= 9) { + lastFlashes++; + levels[i][j] = -1; + for (const [a,b] of neighbours) { + if (!(0 <= i + a && i + a <= levels.length - 1 && 0 <= j + b && j + b <= levels[i].length - 1)) { + continue + } + if (levels[i + a][j + b] != -1) { + levels[i + a][j + b]++; + if (levels[i + a][j + b] > 9) { + coords.push([i + a, j + b]); + } + } + } + } else { + if (levels[i][j] != -1) { + levels[i][j]++; + } + } + } + + for (const i in levels) { + for (const j in levels[i]) { + if (levels[i][j] == -1) { + levels[i][j] = 0; + } + } + } + + } + return generation + +} + +console.log("Part1: " + await Part1(100)); +console.log("Part2: " + await Part2()); \ No newline at end of file diff --git a/Day11/input.txt b/Day11/input.txt new file mode 100644 index 0000000..1d764ea --- /dev/null +++ b/Day11/input.txt @@ -0,0 +1,10 @@ +4836484555 +4663841772 +3512484556 +1481547572 +7741183422 +8683222882 +4215244233 +1544712171 +5725855786 +1717382281