-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem19b.js
178 lines (174 loc) · 4.23 KB
/
problem19b.js
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
const fs = require('fs');
const data = fs.readFileSync('input19.txt', 'utf8');
// const data = `
// 42: 9 14 | 10 1
// 9: 14 27 | 1 26
// 10: 23 14 | 28 1
// 1: "a"
// 11: 42 31
// 5: 1 14 | 15 1
// 19: 14 1 | 14 14
// 12: 24 14 | 19 1
// 16: 15 1 | 14 14
// 31: 14 17 | 1 13
// 6: 14 14 | 1 14
// 2: 1 24 | 14 4
// 0: 8 11
// 13: 14 3 | 1 12
// 15: 1 | 14
// 17: 14 2 | 1 7
// 23: 25 1 | 22 14
// 28: 16 1
// 4: 1 1
// 20: 14 14 | 1 15
// 3: 5 14 | 16 1
// 27: 1 6 | 14 18
// 14: "b"
// 21: 14 1 | 1 14
// 25: 1 1 | 1 14
// 22: 14 14
// 8: 42
// 26: 14 22 | 1 20
// 18: 15 15
// 7: 14 5 | 1 21
// 24: 14 1
// bbabbbbaabaabba
// abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
// babbbbaabbbbbabbbbbbaabaaabaaa
// aaabbbbbbaaaabaababaabababbabaaabbababababaaa
// bbbbbbbaaaabbbbaaabbabaaa
// bbbababbbbaaaaaaaabbababaaababaabab
// ababaaaaaabaaab
// ababaaaaabbbaba
// baabbaaaabbaaaababbaababb
// abbbbabbbbaaaababbbbbbaaaababb
// aaaaabbaabaaaaababaa
// aaaabbaaaabbaaa
// aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
// babaaabbbaaabaababbaabababaaab
// aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba
// `;
const lines = data.split('\n').map(line => line.trim()).filter(line => line.length);
const rules = {};
const inputs = [];
let count = 0;
for (let line of lines) {
if (line === '8: 42') {
line = '8: 42 | 42 8';
count++;
}
if (line === '11: 42 31') {
line = '11: 42 31 | 42 11 31';
count++;
}
let matches = line.match(/^(\d+): "(\w+)"$/);
if (matches) {
const [ dummy, index, text ] = matches;
rules[index] = {
type: 'text',
value: text
};
} else {
matches = line.match(/^(\d+): (.*)$/);
if (matches) {
const [ dummy, index, body ] = matches;
const sets = body.split(' | ').map(s => s.split(' '));
rules[index] = {
type: 'sets',
value: sets
};
} else {
inputs.push(line);
}
}
}
let good = 0;
for (const input of inputs) {
const tokens = input.split('');
let offset = 0;
const cache = {};
try {
if (parse('0', true).length) {
good++;
console.log(`Yes: ${input}`);
}
} catch (e) {
// Nope
}
function parse(index, terminal) {
const key = `${index}:${offset}:${terminal}`;
if (cache[key] !== undefined) {
return cache[key];
}
// console.log(`<< ${JSON.stringify(rules[index], null, ' ')}\n ${tokens} >>`);
const rule = rules[index];
if (rule.type === 'text') {
try {
expect(rule.value);
if (terminal && (offset < tokens.length)) {
return memo([]);
}
} catch (e) {
return memo([]);
}
return memo([ offset ]);
} else {
let returnContinuations = [];
for (const set of rule.value) {
let offsetWas = offset;
let continuations = [ offset ];
for (let i = 0; (i < set.length); i++) {
let nextContinuations = [];
for (let j = 0; (j < continuations.length); j++) {
const index = set[i];
offset = continuations[j];
nextContinuations = [...nextContinuations, ...parse(index, false) ];
}
continuations = [... new Set(nextContinuations)];
}
if (terminal) {
continuations = continuations.filter(continuation => continuation === tokens.length);
}
returnContinuations = [... new Set([ ...returnContinuations, ...continuations ])];
// if (returnContinuations.length) {
// console.log(JSON.stringify(returnContinuations));
// }
offset = offsetWas;
}
return memo(returnContinuations);
}
function memo(value) {
cache[key] = value;
return value;
}
}
function accept(...options) {
const token = tokens[offset];
if (!token) {
return false;
}
for (const option of options) {
if (token === option) {
consume();
return token;
}
if ((option === 'number') && ((typeof token) === option)) {
consume();
return { number: token };
}
}
return false;
}
function expect(option) {
const result = accept(option);
if (!result) {
throw `Expected ${option}`;
}
return result;
}
function consume() {
offset++;
}
}
console.log(`replacements: ${count}`);
console.log(good);