-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsix.js
46 lines (37 loc) · 802 Bytes
/
six.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
import { readFile } from 'fs/promises';
// 6a
async function init() {
const input = await readFile('six.txt', 'utf8');
let FISH = input.split(',').reduce(
(acc, d) => {
acc[d] += 1;
return acc;
},
{ 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0 }
);
let index = 0;
while (index < 256) {
FISH = iterate(FISH);
index++;
}
console.log(total(FISH));
}
function iterate(FISH) {
const nextFish = {};
nextFish[8] = FISH[0] || 0;
nextFish[6] = FISH[0] || 0;
for (let i = 1; i <= 8; i++) {
if (i === 7) {
nextFish[i - 1] += FISH[i];
} else {
nextFish[i - 1] = FISH[i] || 0;
}
}
return nextFish;
}
function total(fish) {
return Object.keys(fish).reduce((acc, d) => {
return acc + fish[d];
}, 0);
}
init();