diff --git a/day06.js b/day06.js new file mode 100644 index 0000000..540f4d0 --- /dev/null +++ b/day06.js @@ -0,0 +1,39 @@ +const fs = require("fs"); + +const fishes = fs + .readFileSync("day06.txt", { encoding: "utf-8" }) // read day??.txt content + .replace(/[\r\n]/g, "") // remove all \r characters to avoid issues on Windows + .split(",") // Split on newline + .map(Number); // Parse each string into a number + +function part1() { + const queue = Array(9).fill(0); + for (const fish of fishes) { + queue[fish]++; + } + for (let i = 0; i < 80; i++) { + const currentFishes = queue.shift(); + queue.push(currentFishes); + queue[6] += currentFishes; + } + + console.log(queue.reduce((a, b) => a + b, 0)); +} + +part1(); + +function part2() { + const queue = Array(9).fill(0); + for (const fish of fishes) { + queue[fish]++; + } + for (let i = 0; i < 256; i++) { + const currentFishes = queue.shift(); + queue.push(currentFishes); + queue[6] += currentFishes; + } + + console.log(queue.reduce((a, b) => a + b, 0)); +} + +part2(); diff --git a/day06.txt b/day06.txt new file mode 100644 index 0000000..1e7a8df --- /dev/null +++ b/day06.txt @@ -0,0 +1 @@ +5,1,1,4,1,1,4,1,1,1,1,1,1,1,1,1,1,1,4,2,1,1,1,3,5,1,1,1,5,4,1,1,1,2,2,1,1,1,2,1,1,1,2,5,2,1,2,2,3,1,1,1,1,1,1,1,1,5,1,1,4,1,1,1,5,4,1,1,3,3,2,1,1,1,5,1,1,4,1,1,5,1,1,5,1,2,3,1,5,1,3,2,1,3,1,1,4,1,1,1,1,2,1,2,1,1,2,1,1,1,4,4,1,5,1,1,3,5,1,1,5,1,4,1,1,1,1,1,1,1,1,1,2,2,3,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,5,1,1,1,1,4,1,1,1,1,4,1,1,1,1,3,1,2,1,2,1,3,1,3,4,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,4,1,1,2,2,1,2,4,1,1,3,1,1,1,5,1,3,1,1,1,5,5,1,1,1,1,2,3,4,1,1,1,1,1,1,1,1,1,1,1,1,5,1,4,3,1,1,1,2,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,3,3,1,2,2,1,4,1,5,1,5,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,5,1,1,1,4,3,1,1,4