-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6question2.ts
More file actions
153 lines (117 loc) · 3.62 KB
/
day6question2.ts
File metadata and controls
153 lines (117 loc) · 3.62 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Big idea is to optimize by grouping each reproductive tier.
// That way, the size of the tier array is constrained
import { readFileSync } from "fs";
const bornReproductionTime = 8;
const birthingReproductionTime = 6;
class LanternFishReproductiveTier {
private _daysTillReproduction: number;
public get daysTillReproduction(): number {
return this._daysTillReproduction;
}
private set daysTillReproduction(value: number) {
this._daysTillReproduction = value;
}
private _numberOfFish: number;
public get numberOfFish(): number {
return this._numberOfFish;
}
protected set numberOfFish(value: number) {
this._numberOfFish = value;
}
constructor(daysTillReproduction: number, numberOfFish: number) {
this.daysTillReproduction = daysTillReproduction;
this.numberOfFish = numberOfFish;
}
public age() {
this.daysTillReproduction--;
}
public shouldReproduce() {
return this.daysTillReproduction < 0;
}
public reproduce() {
this.daysTillReproduction = birthingReproductionTime;
return this.numberOfFish;
}
public increaseTier(numberOfNewFish: number) {
this.numberOfFish += numberOfNewFish;
}
}
let inputs: number[];
const rawData = readFileSync("./day6inputs.txt", "utf8");
inputs = rawData.split(",").map((i) => Number(i));
let lanternFishReproductiveTiers = buildInitialTiers(inputs);
let fishTotal = 0;
const daysToSimulate = 256;
for (let i = 0; i < daysToSimulate; i++) {
let newTier: LanternFishReproductiveTier = new LanternFishReproductiveTier(
bornReproductionTime,
0
);
for (let tier of lanternFishReproductiveTiers) {
tier.age();
if (tier.shouldReproduce()) {
newTier.increaseTier(tier.reproduce());
}
}
if (newTier.numberOfFish !== 0) {
lanternFishReproductiveTiers.push(newTier);
}
lanternFishReproductiveTiers = compressReproductiveTiers(
lanternFishReproductiveTiers
);
}
fishTotal += lanternFishReproductiveTiers.reduce(
(rollingFishTotal, currentTier) => {
return rollingFishTotal + currentTier.numberOfFish;
},
0
);
console.log(fishTotal);
function buildInitialTiers(inputs: number[]) {
let initialLanternFishReduction = inputs.reduce<Map<number, number>>(
(latestReduction, currentValue) => {
let input = currentValue;
latestReduction.set(
input,
latestReduction.has(input) ? latestReduction.get(input) + 1 : 1
);
return latestReduction;
},
new Map<number, number>()
);
return Array.from(initialLanternFishReduction).map(
([generationNumber, numberOfFish]) =>
new LanternFishReproductiveTier(generationNumber, numberOfFish)
);
}
function compressReproductiveTiers(
lanternFishReproductiveTiers: LanternFishReproductiveTier[]
) {
let compressedTiers = [];
for (let i = 0; i <= bornReproductionTime; i++) {
let tierDuplicates = lanternFishReproductiveTiers.filter(
(t) => t.daysTillReproduction === i
);
switch (tierDuplicates.length) {
case 0: {
break;
}
case 1: {
compressedTiers.push(tierDuplicates[0]);
break;
}
default: {
let compressedTierFishCount = tierDuplicates.reduce(
(rollingFishTotal, currentTierDuplicate) => {
return rollingFishTotal + currentTierDuplicate.numberOfFish;
},
0
);
compressedTiers.push(
new LanternFishReproductiveTier(i, compressedTierFishCount)
);
}
}
}
return compressedTiers;
}