-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle12.ts
295 lines (255 loc) · 8.31 KB
/
puzzle12.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { readFile } from 'node:fs/promises';
import path from 'node:path';
export async function solvePuzzleBase() {
const content = await readFile(path.join('./input/puzzle12.txt'), {encoding: 'utf8'});
const lines = content.split('\n').filter(line => line);
const field = parseSpringField(lines);
const matcher = new Matcher();
let totalVariants = 0;
for (const row of field) {
totalVariants += naiveCountVariants(row, matcher);
}
console.log(`Puzzle 12: ${totalVariants}`);
}
export async function solvePuzzleAdvanced() {
const content = await readFile(path.join('./input/puzzle12.txt'), {encoding: 'utf8'});
const lines = content.split('\n').filter(line => line);
let field = parseSpringField(lines);
field = unfoldSpringField(field, 5);
const matcher = new Matcher();
let totalMemoized = 0;
let totalReduced = 0;
for (const row of field) {
totalMemoized += memoizedCountVariants(row, matcher);
totalReduced += reducedCountVariants(row, matcher);
}
console.log(
`Puzzle 12 (advanced):\n` +
` memoized = ${totalMemoized}\n` +
` reduced = ${totalReduced}`
);
}
interface SpringRow {
readonly index: number;
readonly springs: string;
readonly groups: readonly number[];
readonly totalDamaged: number;
}
function parseSpringField(lines: readonly string[]): SpringRow[] {
return lines.map((line, index): SpringRow => {
const [springs, groupsText] = line.split(' ');
const groups = groupsText.split(',').map(n => Number(n));
let totalDamaged = 0;
for (const group of groups) {
totalDamaged += group;
}
return {index, springs, groups, totalDamaged};
});
}
function unfoldSpringField(field: readonly SpringRow[], times: number): SpringRow[] {
return field.map((row): SpringRow => {
const {index, springs, groups, totalDamaged} = row;
return {
index,
springs: Array.from({length: times}, () => springs).join('?'),
groups: Array.from({length: times}, () => groups).flat(),
totalDamaged: totalDamaged * times,
};
});
}
class Matcher {
private damagedCountToRegex: RegExp[] = [];
private finalRegex = /[?.]*$/y;
private getDamagedCountRegex(n: number) {
while (this.damagedCountToRegex.length < n + 1) {
const index = this.damagedCountToRegex.length;
this.damagedCountToRegex.push(new RegExp(`[?#]{${index}}(?=[\\.?]|$)`, 'g'));
}
return this.damagedCountToRegex[n];
}
matchGroup(springs: string, startOffset: number, size: number): number {
const groupRegex = this.getDamagedCountRegex(size);
groupRegex.lastIndex = startOffset;
const match = groupRegex.exec(springs);
if (match) {
return match.index;
}
return -1;
}
matchFinal(springs: string, endOffset: number): boolean {
const {finalRegex} = this;
finalRegex.lastIndex = endOffset;
return finalRegex.test(springs);
}
matchSpace(springs: string, start: number, to: number): boolean {
const damaged = springs.indexOf('#', start);
return (damaged < 0 || damaged >= to);
}
}
function naiveCountVariants(row: SpringRow, matcher: Matcher): number {
const {springs, groups} = row;
if (groups.length === 0) {
return 0;
}
let variants = 0;
const offsets: number[] = [0];
while (offsets.length > 0) {
const offset = offsets.pop()!;
const groupIndex = offsets.length;
const groupSize = groups[groupIndex];
const previousEnd = previousEndOffset(groups, offsets, groupIndex);
const matchStart = matcher.matchGroup(springs, offset, groupSize);
if (matchStart >= 0 && matcher.matchSpace(springs, previousEnd, matchStart)) {
const matchEnd = matchStart + groupSize;
if (groupIndex === groups.length - 1) {
if (matcher.matchFinal(springs, matchEnd)) {
variants++;
}
offsets.push(matchStart + 1);
} else {
const nextGroupOffset = matchEnd + 1;
if (nextGroupOffset < springs.length) {
offsets.push(
matchStart + 1,
nextGroupOffset
);
}
}
}
}
return variants;
}
function previousEndOffset(
groups: readonly number[],
offsets: readonly number[],
index: number
): number {
if (index === 0) {
return 0;
}
return offsets[index - 1] + groups[index - 1];
}
/**
* Initial solution for Part 2 which does not use any memoization.
*/
function reducedCountVariants(row: SpringRow, matcher: Matcher): number {
const {springs, groups} = row;
const reverseRow: SpringRow = {
index: -row.index,
springs: [...springs].reverse().join(''),
groups: [...groups].reverse(),
totalDamaged: row.totalDamaged,
};
const minStarts = computeMinOffsets(row, matcher)
.map(offset => offset - 1);
const maxEnds = computeMinOffsets(reverseRow, matcher)
.map(offset => row.springs.length - offset + 1)
.reverse();
interface EndTuple {
readonly end: number;
readonly combinations: number;
}
let lastEnds: EndTuple[] = [];
for (let i = 0; i < groups.length; i++) {
const group = groups[i];
const start = minStarts[i];
const end = maxEnds[i];
const segment = springs.substring(start, end);
const matches: number[] = [];
let nextMatch = 0;
while (true) {
const matchStart = matcher.matchGroup(segment, nextMatch, group);
if (matchStart < 0) {
break;
}
matches.push(start + matchStart);
nextMatch = matchStart + 1;
}
const nextEnds: EndTuple[] = [];
for (let j = 0; j < matches.length; j++) {
const matchOffset = matches[j];
let lastEndCombinations = i === 0 ? 1 : 0;
for (const {end, combinations} of lastEnds) {
if (end < matchOffset && matcher.matchSpace(springs, end, matchOffset)) {
lastEndCombinations += combinations;
}
}
if (lastEndCombinations > 0) {
nextEnds.push({
end: matchOffset + group,
combinations: lastEndCombinations,
});
}
}
lastEnds = nextEnds;
}
let combinations = 0;
for (const lastEnd of lastEnds) {
combinations += lastEnd.combinations;
}
return combinations;
}
function computeMinOffsets(row: SpringRow, matcher: Matcher): number[] {
const {groups, springs} = row;
const offsets: number[] = [0];
while (offsets.length > 0) {
const offset = offsets.pop()!;
const groupIndex = offsets.length;
const groupSize = groups[groupIndex];
const previousEnd = previousEndOffset(groups, offsets, groupIndex);
const matchStart = matcher.matchGroup(springs, offset, groupSize);
if (matchStart >= 0 && matcher.matchSpace(springs, previousEnd, matchStart)) {
const matchEnd = matchStart + groupSize;
if (groupIndex === groups.length - 1) {
if (matcher.matchFinal(springs, matchEnd)) {
offsets.push(matchStart + 1);
return offsets;
}
offsets.push(matchStart + 1);
} else {
const nextGroupOffset = matchEnd + 1;
if (nextGroupOffset < springs.length) {
offsets.push(
matchStart + 1,
nextGroupOffset
);
}
}
}
}
throw new Error(`Failed to find any matches for row ${row.index}`);
}
function memoizedCountVariants(row: SpringRow, matcher: Matcher): number {
const memoizedCounts = new Map<`${number}:${number}`, number>();
function countSubGroups(subGroup: number, subOffset: number): number {
const memoizedResult = memoizedCounts.get(`${subGroup}:${subOffset}`);
if (memoizedResult !== undefined) {
return memoizedResult;
}
const groupSize = row.groups[subGroup];
let combinations = 0;
let nextStart = subOffset;
while (true) {
const matchStart = matcher.matchGroup(row.springs, nextStart, groupSize);
if (matchStart < 0 || !matcher.matchSpace(row.springs, subOffset, matchStart)) {
break;
}
nextStart = matchStart + 1;
if (subGroup === row.groups.length - 1) {
if (matcher.matchFinal(row.springs, matchStart + groupSize)) {
combinations++;
}
} else {
const nestedCount = countSubGroups(subGroup + 1, matchStart + groupSize + 1);
combinations += nestedCount;
}
}
memoizedCounts.set(`${subGroup}:${subOffset}`, combinations);
return combinations;
}
return countSubGroups(0, 0);
}
(async function main() {
await solvePuzzleBase();
await solvePuzzleAdvanced();
})();