-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6question1.ts
More file actions
51 lines (34 loc) · 1.01 KB
/
day6question1.ts
File metadata and controls
51 lines (34 loc) · 1.01 KB
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
import { readFileSync } from "fs";
class LanternFish {
private daysTillReproduction: number;
constructor(daysTillReproduction: number) {
this.daysTillReproduction = daysTillReproduction;
}
public age() {
this.daysTillReproduction--;
}
public shouldReproduce() {
return this.daysTillReproduction < 0;
}
public reproduce() {
this.daysTillReproduction = 6;
return new LanternFish(8);
}
}
let inputs: string[];
const rawData = readFileSync("./day6inputs.txt", "utf8");
inputs = rawData.split(",");
let lanternFish = inputs.map((i) => new LanternFish(Number(i)));
const daysToSimulate = 8;
for (let i = 0; i < daysToSimulate; i++) {
let newLanternfish: LanternFish[] = [];
for (let fish of lanternFish) {
fish.age();
if (fish.shouldReproduce()) {
newLanternfish.push(fish.reproduce());
}
}
lanternFish = lanternFish.concat(newLanternfish);
newLanternfish.length = 0;
}
console.log(lanternFish.length);