-
Notifications
You must be signed in to change notification settings - Fork 0
/
multifish.js
103 lines (83 loc) · 2.44 KB
/
multifish.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
/**
* ___ __ __
* | \ __ _ _ _ / \ / /
* | |) / _` | || | | () / _ \
* |___/\__,_|\_, | \__/\___/
* |__/
*
* "Lanternfish"
*
* Challenge:
* Consider a school of fish each identified by the number of cycles until they
* next reproduce. Given a list of such numbers, grow the school a given number
* of cycles and count the fish present on a particular day.
*
* - When a fish is born, it starts at 8 days (counting down)
* - When a fish reaches 0 days, the _next_ morning it produces a fish at 8 days
* and its own counter resets to 6.
* - 0 is a valid day. Fish make fish after Day 0, not after Day 1.
*/
const fs = require('fs');
const days = 256;
const input = fs.readFileSync('input.txt')
.toString()
.trim();
const fishies = input.split(',').map(f => parseInt(f));
const day = () => {
// Subtract one from the cycle time on each fish.
fishies.forEach((f, i) => fishies[i] -= 1);
// Reset reproduction cycle and count the fish that were born.
let birthdays = 0;
fishies.forEach((f, i) => {
if (f === -1) {
birthdays += 1;
fishies[i] = 6;
}
});
// Make new fishies and push 'em into the school.
Array(birthdays).fill(8).map(f => fishies.push(f));
};
// Part One code crashes in Part Two...
if (days <= 80) {
for (let i = days; i > 0, i--;) {
day();
}
console.log(`After ${days}, there would be ${fishies.length} fish.`);
}
// Part One:
// After 80, there would be 390923 fish.
/**
* ___ _ ___
* | _ \__ _ _ _| |_ |_ )
* | _/ _` | '_| _| / /
* |_| \__,_|_| \__| /___|
*
* Repeat the fish reproduction cycle until all system memory is sushi.
*
* Or: how many fish after day 256?
*/
// What if I maintained a count of fish by age instead?
const newSchool = Array(9).fill(0);
fs.readFileSync('input.txt')
.toString()
.trim()
.split(',')
.map(f => parseInt(f))
.forEach((f, i) => { newSchool[f] += 1 });
/**
* Take any 0 Day fish off the newSchool. That number would be both the number
* of new fish (8 Day fish) and parents (6 Day fish).
*/
const cycle = () => {
const newFish = newSchool.shift();
newSchool.push(newFish);
newSchool[6] += newFish;
};
const countFishies = () => newSchool.reduce((acc, age) => acc + age);
for (let i = 1; i <= days; i++) {
cycle();
}
console.log(`After ${days} days, there would be ${countFishies()} fish.`);
// Part Two:
// After 256 days, there would be 1749945484935 fish.