AdventOfCode2021/Day6/Reproduction.ts
2023-06-07 16:32:15 +02:00

41 lines
886 B
TypeScript

import { readLines } from "https://deno.land/std@0.177.1/io/read_lines.ts";
function step(fish: number[]) {
const result = [0,0,0,0,0,0,0,0,0];
for (let i = 1; i <= 8; i++) {
result[i-1] = fish[i];
}
result[8] = fish[0];
result[6] += fish[0];
return result;
}
async function simulate(rounds: number) {
const file = await Deno.open("./Day6/input.txt");
const lines = readLines(file);
let line: string = (await lines.next()).value;
let fish = [0,0,0,0,0,0,0,0,0]
for (let s of line.split(",")) {
fish[+s] += 1;
}
for (let i = 0; i < rounds; i++) {
fish = step(fish);
}
return fish.reduce((a,b) => a + b)
}
async function Part1() {
return simulate(80);
}
async function Part2() {
return simulate(256);
}
console.log("Part1: " + await Part1());
console.log("Part1: " + await Part2());