-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.ts
186 lines (156 loc) · 3.91 KB
/
day12.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
import { PerformanceMark, PerformanceMeasure } from 'perf_hooks';
const test_input = `???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1`.split('\n');
const puzzle_input = `<get from aoc website>`.split('\n');
function log(u: unknown) {
console.log(JSON.stringify(u));
}
function numStringToArray(src: string): number[] {
return src
.split(',')
.filter((s) => s.trim().length > 0)
.map(Number);
}
function validPositions(template: string, groups: number[]): number {
let sum = 0;
const q: [[string, number[]]] = [[template, groups]];
while (q.length > 0) {
const [t, g] = q.pop() ?? ['', []];
// log({ t, g });
if (t.length === 0) {
if (g.length === 0) {
sum += 1;
continue;
}
}
if (g.length === 0) {
if (!t.includes('#')) {
sum += 1;
continue;
}
}
if (t.length >= g.reduce((a, c) => a + c, 0) + g.length - 1) {
const c = t[0];
if (c === '?') {
q.push(['.' + t.slice(1), g]);
q.push(['#' + t.slice(1), g]);
}
if (c === '.') {
let i = 1;
for (; i < t.length; i++) {
if (t[i] !== '.') {
break;
}
}
q.push([t.slice(i), g]);
}
if (c === '#') {
const [cr, ...rem] = g;
let i = 0;
for (; i < cr; i++) {
if (t[i] === '.') {
break;
}
}
if (i === cr) {
if (t[i] !== '#') {
q.push([t.slice(cr + 1), rem]);
}
}
}
}
}
return sum;
}
const cache: Map<string, number> = new Map();
function validPositionsRecurse(template: string, groups: number[]): number {
const key = JSON.stringify({ template, groups });
const entry = cache.get(key);
if (entry != null) {
return entry;
}
const [t, g] = [template, groups];
// log({ t, g });
if (t.length === 0) {
if (g.length === 0) {
cache.set(key, 1);
return 1;
}
}
if (g.length === 0) {
if (!t.includes('#')) {
cache.set(key, 1);
return 1;
}
}
if (t.length >= g.reduce((a, c) => a + c, 0) + g.length - 1) {
const c = t[0];
if (c === '?') {
const sum =
validPositionsRecurse('.' + t.slice(1), g) +
validPositionsRecurse('#' + t.slice(1), g);
cache.set(key, sum);
return sum;
}
if (c === '.') {
let i = 1;
for (; i < t.length; i++) {
if (t[i] !== '.') {
break;
}
}
const sum = validPositionsRecurse(t.slice(i), g);
cache.set(key, sum);
return sum;
}
if (c === '#') {
const [cr, ...rem] = g;
let i = 0;
for (; i < cr; i++) {
if (t[i] === '.') {
break;
}
}
if (i === cr) {
if (t[i] !== '#') {
const sum = validPositionsRecurse(t.slice(cr + 1), rem);
cache.set(key, sum);
return sum;
}
}
}
}
return 0;
}
function part1(input: string[]) {
const counts: number[] = input.map((r) => {
const [template, groupString] = r.split(' ');
const groups = numStringToArray(groupString);
return validPositions(template, groups);
});
log(counts);
log(counts.reduce((a, c) => a + c));
}
function part2(input: string[]) {
const expanded: [string, number[]][] = input.map((r) => {
const [t, g] = r.split(' ');
const groupString = new Array<string>(5).fill(g.trim()).join(',');
const newT = new Array<string>(5).fill(t).join('?');
const newG = numStringToArray(groupString);
return [newT, newG];
});
// log(expanded);
const counts: number[] = expanded.map((r, i) => {
log(`${i + 1} / ${expanded.length}`);
const [template, groups] = r;
return validPositionsRecurse(template, groups);
});
log(counts);
log(counts.reduce((a, c) => a + c));
}
// part1(puzzle_input);
part2(puzzle_input);