-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
196 lines (173 loc) · 4.24 KB
/
solution.nim
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
import ../../lib/imports
type
RuleKind {.pure.} = enum
Char
Seqs
Rule = object
id: int
case kind: RuleKind
of RuleKind.Char:
ch: char
of RuleKind.Seqs:
seqs: seq[seq[int]]
proc parseLine(line: string): Rule =
var parts = line.split(": ")
let id = parts[0].parseInt
if parts.len == 2 and parts[1].startsWith("\""):
let ch = parts[1][1]
return Rule(kind: RuleKind.Char, id: id, ch: ch)
var seqs = newSeq[seq[int]]()
for p in parts[1].split(" | "):
seqs.add p.split(" ").mapIt(it.parseInt)
Rule(kind: RuleKind.Seqs, id: id, seqs: seqs)
proc parse(input: string): (Table[int, Rule], seq[string]) =
let parts = input.split("\n\n")
let rules = parts[0].split("\n").mapIt(it.parseLine)
let msgs = parts[1].split("\n")
var rulesMap = initTable[int, Rule]()
for rule in rules: rulesMap[rule.id] = rule
(rulesMap, msgs)
when defined(test):
let input = """
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
""".strip
block:
let (rules, msgs) = input.parse
doAssert rules[0].seqs == @[@[4, 1, 5]]
doAssert rules[1].seqs == @[@[2, 3], @[3, 2]]
doAssert rules[2].seqs == @[@[4, 4], @[5, 5]]
doAssert rules[3].seqs == @[@[4, 5], @[5, 4]]
doAssert rules[4].ch == 'a'
doAssert rules[5].ch == 'b'
doAssert msgs.len == 5
#[
0
4 1 5
a 3 2 b
5 4 5 5
b a b b
]#
proc matches(s: string, rulesMap: sink Table[int, Rule], start: int): bool =
proc matches(r, i: int): (bool, int) =
if i >= s.len: return (false, i)
let rule = rulesMap[r]
if rule.kind == RuleKind.Char:
if rule.ch == s[i]: return (true, i + 1)
return (false, i)
else: # RuleKind.Seqs
for altRules in rule.seqs:
var allMatched = true
var j = i
var matched: bool
for r in altRules:
(matched, j) = matches(r, j)
if not matched:
allMatched = false
break
if allMatched:
return (true, j)
return (false, i)
let (matched, i) = matches(start, 0)
matched and i == s.len
when defined(test):
block:
let (rules, msgs) = input.parse
for i, e in [true, false, true, false, false]:
doAssert msgs[i].matches(rules, 0) == e
proc part1(input: string): int =
let (rules, msgs) = input.parse
msgs.countIt(it.matches(rules, 0))
when defined(test):
block:
doAssert part1(input) == 2
when defined(test):
let input2 = """
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
""".strip
block:
doAssert part1(input2) == 3
proc canMatch(msg: string, rules: sink Table[int, Rule], thres: int): bool =
for i in 1 .. thres:
for j in 1 .. thres:
block:
var res = newSeq[seq[int]]()
var r = newSeq[int]()
for _ in 0 ..< i: r.add 42
res.add r
rules[8] = Rule(id: 8, kind: RuleKind.Seqs, seqs: res)
block:
var res = newSeq[seq[int]]()
var r = newSeq[int]()
for _ in 0 ..< j: r.add 42
for _ in 0 ..< j: r.add 31
res.add r
rules[11] = Rule(id: 11, kind: RuleKind.Seqs, seqs: res)
if msg.matches(rules, 0): return true
proc part2(input: string): int =
let (rules, msgs) = input.parse
const thres = 10
for msg in msgs:
if canMatch(msg, rules, thres):
result += 1
when defined(test):
block:
doAssert part2(input2) == 12
when isMainModule and not defined(test):
let input = readFile("input").strip
echo part1(input)
echo part2(input)