-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_19.js
187 lines (162 loc) · 4.1 KB
/
day_19.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
179
180
181
182
183
184
185
186
187
'use strict'
const THIS = 'day_19'
const rawInput = []
// The actual input
rawInput[0] = require('./data/' + THIS)
// The 1-st example
rawInput[1] = `0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b"
ababbb
bababa
abbbab
aaabbb
aaaabbb`
// The 2-nd example
rawInput[2] = `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
abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
bbabbbbaabaabba
babbbbaabbbbbabbbbbbaabaaabaaa
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
bbbbbbbaaaabbbbaaabbabaaa
bbbababbbbaaaaaaaabbababaaababaabab
ababaaaaaabaaab
ababaaaaabbbaba
baabbaaaabbaaaababbaababb
abbbbabbbbaaaababbbbbbaaaababb
aaaaabbaabaaaaababaa
aaaabbaaaabbaaa
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
babaaabbbaaabaababbaabababaaab
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba`
const { assert, execute } = require('./execute')
assert.hook(() => {
console.log('--- BREAKPOINT ---') // Yeah, sometimes I have to use this!
})
const inputLines = []
const lexemes = [], rules = new Map()
const dictionary = new Map(), reverse = new Map()
let visited = new Set(), show
// Returns the length of match from the beginning.
const match = (rule, input) => {
let res = 0, key
if (typeof rule === 'number') { // Just a rule ID.
key = rule + ':' + input.map(v => reverse.get(v)).join('')
// if (rule === 11) console.log('B', key)
if (!visited.has(key)) {
visited.add(key) // Detect if a rule with the same input is cycling...
if (lexemes.includes(rule)) { // The rule ID is a terminal symbol.
if (rule === input[0]) res = 1
} else {
res = match(rules.get(rule), input)
}
} else {
console.log('REC', key) // ... this never happened!
key = undefined
}
} else {
if (typeof rule[0] === 'number') { // Array of rule ID-s: all must match.
let index = 0
for (const part of rule) {
if (!(res = match(part, input.slice(index)))) break
index += res
}
if (res) res = index
} else { // Array of rules: just one must match.
for (const part of rule) {
if ((res = match(part, input))) break
}
}
}
if (show && res && typeof rule !== 'number') console.log(rule, input, '->', res)
if (key) {
visited.delete(key)
}
// if (rule === 11) console.log('E', rule, res)
return res
}
const makeRules = (input) => {
let line, i = 0, r
while ((line = input[i++]) !== undefined) {
if ((r = /^(\d+):\s+(.+)$/.exec(line))) {
let ruleNum = r[1] * 1, rule
if ((r[2][0] * 1) >= 0) {
rule = r[2].split('|').map(p => p.trim().split(' ').map(s => 1 * s))
rules.set(ruleNum, rule)
} else {
assert((r = /^"(\S)"/.exec(r[2])), 'syntax2')
dictionary.set(r[1], ruleNum)
reverse.set(ruleNum, r[1])
lexemes.push(ruleNum)
}
} else {
assert(line === '', 'syntax1')
break
}
}
return i
}
const compute = (input) => {
let sum = 0
for (let i = 0; (input[i]) !== undefined; ++i) {
visited.clear()
let y = match(0, input[i]), l = input[i].length
// console.log('***** %i: %s', i, input[i].map(v => reverse.get(v)).join(''), '--->', y, l, y === l)
y = y === l
if (y) {
++sum
// console.log('***** %i: %s', i, input[i].map(v => reverse.get(v)).join(''))
}
}
return sum
}
const compute1 = (dataSet) => {
let input = rawInput[dataSet].split('\n')
input = input.slice(makeRules(input))
input.forEach(line => inputLines.push(Array.from(line).map(v => dictionary.get(v))))
return compute(inputLines)
}
const compute2 = () => {
makeRules(['8: 42 | 42 8', '11: 42 31 | 42 11 31'])
return compute(inputLines)
}
execute('puzzle #1', compute1)
execute('puzzle #2', compute2)
/*
0: 203
1: 225 wrong
*/
// by #1: 3305 1879