-
Notifications
You must be signed in to change notification settings - Fork 17
/
part-two.js
39 lines (32 loc) · 1 KB
/
part-two.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 { input } = require("./input");
/**
* Fish can only have an age of 0 - 8, so create an array of 9 items
* and store the _count_ of all fish in that age.
*/
const days = Array(9).fill(0);
for (let day of input) {
days[day]++;
}
for (let i = 0; i < 256; i++) {
let six_fish = 0;
let eight_fish = 0;
for (let day = 0; day < days.length; day++) {
let count = days[day];
if (day === 0) {
// Double the fish that are at age zero
// The same fish loop back around to age 6
six_fish = count;
// The "spawned" fish get an age of 8
eight_fish = count;
} else {
// Otherwise, "shift" each group of fish to the left (e.g. get 1 day younger)
days[day - 1] = count;
}
}
// After a complete day, add the "looped back" fish at age 6 to our array
days[6] += six_fish;
// Set the fish at age 8 to the number of fish that were spawned (don't add, because we don't "shift" in a 0 for day 8's slot)
days[8] = eight_fish;
}
const all_fish = days.reduce((a, b) => a + b);
console.log(all_fish);