add Day11
This commit is contained in:
parent
013f1f779c
commit
326760eb72
127
Day11/Octopus.ts
Normal file
127
Day11/Octopus.ts
Normal file
@ -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());
|
||||
10
Day11/input.txt
Normal file
10
Day11/input.txt
Normal file
@ -0,0 +1,10 @@
|
||||
4836484555
|
||||
4663841772
|
||||
3512484556
|
||||
1481547572
|
||||
7741183422
|
||||
8683222882
|
||||
4215244233
|
||||
1544712171
|
||||
5725855786
|
||||
1717382281
|
||||
Loading…
x
Reference in New Issue
Block a user