-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.mjs
66 lines (54 loc) · 1.73 KB
/
tasks.mjs
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
// Lanternfish: Map growth of lanternfish population
// REAL DATA
import input from './input.mjs';
// TEST DATA
// const input = [3, 4, 3, 1, 2];
// Task 1 was simple and just does basic loops to track every single fish. Easy to write and read, but not very efficient. It's slow and memory intensive.
function simulateLife1(input) {
let days = 80;
let population = input;
while (days > 0) {
population = population.flatMap((num) => {
if (num > 0) return num - 1;
return [6, 8];
});
days--;
}
return population.length;
}
// Task 2 is impossible to do with the method used in task 1. So instead this groups fish on the same cycle together and treats them as a single fish. This method takes less time to do 256 days than the last took to do 80 days
function simulateLife2(input) {
let population = objectifyFish(input);
let days = 80;
while (days > 0) {
population = Object.entries(population).reduce(
(acc, [daysTilSpawn, num]) => {
if (daysTilSpawn > 0) {
if (!acc[daysTilSpawn - 1]) acc[daysTilSpawn - 1] = 0;
acc[daysTilSpawn - 1] += num;
} else {
if (!acc['6']) acc['6'] = 0;
acc['6'] += num;
acc['8'] = num;
}
return acc;
},
{},
);
days--;
}
return Object.values(population).reduce((acc, val) => acc + val);
}
function objectifyFish(input) {
return input.reduce((acc, fish) => {
if (!acc[fish]) acc[fish] = 0;
acc[fish]++;
return acc;
}, {});
}
console.time('simulateLife');
console.log('80 Day Population:', simulateLife1(input));
console.timeEnd('simulateLife');
console.time('simulateLife2');
console.log('256 Day Population:', simulateLife2(input));
console.timeEnd('simulateLife2');