-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathinput.js
60 lines (52 loc) · 1.27 KB
/
input.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
const path = require('path');
const fs = require('fs');
function tokenizeRule(rule) {
// @example '53: 86 6 | 52 134' -> parts: ['(', 86, 6, '|', 52, 134, ')']
// @example '0: 8 11' -> parts: ['(', 8, 11, ')']
// @example '86: "a"' parts: 'a'
let [id, parts] = rule.split(': ');
id = parseInt(id, 10);
if (parts.includes('"')) {
// parts === '"b"'
parts = parts[1];
} else {
parts = ['(?:', ...parts.split(' ').map((v) => (/\d+/.test(v) ? parseInt(v, 10) : v)), ')'];
}
return {
id,
parts,
};
}
function splitUpChunks(str) {
return str
.toString()
.trim()
.split('\n\n')
.map((chunk) => chunk.split('\n'));
}
const [rules_raw, codes_raw] = splitUpChunks(
fs.readFileSync(path.join(__dirname, 'input.txt'), 'utf8')
);
/**
* @example rules = { 0: { id, parts }, 1: ... }
*/
const rules = rules_raw
.map((r) => tokenizeRule(r))
.reduce((obj, rule) => ((obj[rule.id] = rule), obj), {});
const codes = codes_raw;
const [sample_rules_raw, sample_codes_raw] = splitUpChunks(`0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b"`);
module.exports = {
tokenizeRule,
input: { rules, codes },
sampleInput: {
rules: sample_rules_raw
.map((r) => tokenizeRule(r))
.reduce((obj, rule) => ((obj[rule.id] = rule), obj), {}),
codes: sample_codes_raw,
},
};