forked from tpatel/advent-of-code-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.js
39 lines (32 loc) · 910 Bytes
/
day06.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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();