-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14question2.ts
More file actions
167 lines (127 loc) · 4.15 KB
/
day14question2.ts
File metadata and controls
167 lines (127 loc) · 4.15 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
class PairInsertionRule {
private pair: string;
private _insertedCharacter: string;
public get insertedElement(): string {
return this._insertedCharacter;
}
private set insertedElement(value: string) {
this._insertedCharacter = value;
}
constructor(pair: string, insertedCharacter: string) {
this.pair = pair;
this.insertedElement = insertedCharacter;
}
public applies(pair: string) {
return this.pair === pair;
}
}
class FrequencyTracker {
private pairFrequency: Map<string, number>;
private _elementFrequency: Map<string, number>;
public get elementFrequency(): Map<string, number> {
return this._elementFrequency;
}
private set elementFrequency(value: Map<string, number>) {
this._elementFrequency = value;
}
constructor(template: string) {
this.pairFrequency = this.buildInitialPairFrequency(template);
this.elementFrequency = this.buildInitialElementFrequency(template);
}
public resetPairFrequency() {
const existingPairFrequency = this.pairFrequency;
this.pairFrequency = new Map();
return existingPairFrequency;
}
private buildInitialPairFrequency(template: string) {
const pairFrequency = new Map();
for (let i = 0; i < template.length - 1; i++) {
let pair = template.slice(i, i + 2);
pairFrequency.set(
pair,
pairFrequency.has(pair) ? pairFrequency.get(pair) + 1 : 1
);
}
return pairFrequency;
}
private buildInitialElementFrequency(template: string) {
const elementFrequency = new Map();
for (let element of template) {
elementFrequency.set(
element,
elementFrequency.has(element) ? elementFrequency.get(element) + 1 : 1
);
}
return elementFrequency;
}
public increasePairFrequency(pair: string, numberOfNewPairs: number) {
this.pairFrequency.set(
pair,
this.pairFrequency.has(pair)
? this.pairFrequency.get(pair) + numberOfNewPairs
: numberOfNewPairs
);
}
public increaseElementFrequency(
element: string,
numberOfNewElements: number
) {
this.elementFrequency.set(
element,
this.elementFrequency.has(element)
? this.elementFrequency.get(element) + numberOfNewElements
: 1
);
}
}
class PolymerProcessor {
private insertionRules: PairInsertionRule[];
private frequencyTracker: FrequencyTracker;
constructor(insertionRules: PairInsertionRule[], template: string) {
this.insertionRules = insertionRules;
this.frequencyTracker = new FrequencyTracker(template);
}
public mutate() {
this.frequencyTracker.resetPairFrequency().forEach((frequency, pair) => {
let applicableRule = this.insertionRules.find((r) => r.applies(pair));
if (applicableRule === undefined) {
throw new Error("Found no rule to process pair: " + pair);
}
this.frequencyTracker.increaseElementFrequency(
applicableRule.insertedElement,
frequency
);
this.frequencyTracker.increasePairFrequency(
pair[0] + applicableRule.insertedElement,
frequency
);
this.frequencyTracker.increasePairFrequency(
applicableRule.insertedElement + pair[1],
frequency
);
}, this);
}
public getElementFrequencies() {
return this.frequencyTracker.elementFrequency;
}
}
import { readFileSync } from "fs";
let inputs: string[];
const rawData = readFileSync("./day14inputs.txt", "utf8");
inputs = rawData.split("\r\n").filter((i) => i !== "");
let template = inputs.shift();
let insertionRules = inputs.map((i) => {
let [pair, insertionCharacter] = i.split(" -> ");
return new PairInsertionRule(pair, insertionCharacter);
});
let processor = new PolymerProcessor(insertionRules, template);
const steps = 40;
for (let i = 0; i < steps; i++) {
processor.mutate();
}
const orderedFrequencies = Array.from(
processor.getElementFrequencies().values()
).sort((a, b) => b - a);
console.log(
orderedFrequencies[0] - orderedFrequencies[orderedFrequencies.length - 1]
);