-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.js
58 lines (40 loc) · 1.09 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const fs = require("fs");
const array = fs
.readFileSync("day06.txt", { encoding: "utf-8" })
.split(",")
.map((x) => Number(x));
const myFunction = (array) => {
let lanternfishes = [...array];
for (let days = 0; days < 80; days++) {
let newborn = 0;
for (let i = 0; i < lanternfishes.length; i++) {
if (lanternfishes[i] === 0) {
lanternfishes[i] = 6;
newborn++;
} else {
lanternfishes[i]--;
}
}
for (let i = 0; i < newborn; i++) {
lanternfishes.push(8);
}
}
return lanternfishes.length;
};
const value = myFunction(array);
console.log(value);
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //
const myFunction2 = (array) => {
const lanternfishes = Array(9).fill(0);
for (const i of array) {
lanternfishes[i]++;
}
for (let i = 0; i < 256; i++) {
const currentFishes = lanternfishes.shift();
lanternfishes.push(currentFishes);
lanternfishes[6] += currentFishes;
}
return lanternfishes.reduce((a, b) => a + b, 0);
};
const value2 = myFunction2(array);
console.log(value2);