-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday07.html
478 lines (429 loc) Β· 29.6 KB
/
day07.html
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2020\day07.nim</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>π³</text></svg>">
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel='stylesheet' href='https://unpkg.com/normalize.css/'>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
<link rel='stylesheet' href='https://cdn.jsdelivr.net/gh/pietroppeter/nimib/assets/androidstudio.css'>
<style>
.nb-box {
display: flex;
align-items: center;
justify-content: space-between;
}
.nb-small {
font-size: 0.8rem;
}
button.nb-small {
float: right;
padding: 2px;
padding-right: 5px;
padding-left: 5px;
}
section#source {
display:none
}
</style>
<script async defer data-domain="pietroppeter.github.io/adventofnim" src="https://plausible.io/js/plausible.js"></script>
<style>
a {
text-decoration: none;
color: #009900;
}
a:hover {
color: #99ff99;
}
em {
color: #ffffff;
font-style: normal;
text-shadow: 0 0 5px #ffffff;
}
em.star {
font-style: normal;
color: #ffff66;
text-shadow: 0 0 5px #ffff66;
}
</style>
</head>
<body>
<header>
<div class="nb-box">
<span><a href="..">π‘</a></span>
<span><code>2020\day07.nim</code></span>
<span><a href="https://github.com/pietroppeter/adventofnim"><svg aria-hidden="true" width="1.2em" height="1.2em" style="vertical-align: middle; fill: #fff" preserveAspectRatio="xMidYMid meet" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"></path></svg></a></span>
</div>
<hr>
</header><main>
<h2>Day 7: Handy Haversacks</h2>
<h3>General strategy</h3>
<p>The solution will be in 5 parts:</p>
<ol>
<li>colors: I will use global variables</li>
<li>parse a single rule: manual parsing (no regex, no npeg - tried but did not succeed at first try)</li>
<li>reversing the rules: while parsing the rules I will also create the mapping
from a color to its direct containers.</li>
<li>solve part 1: recursive</li>
<li>solve part 2: recursive</li>
</ol>
<h3>1. Colors</h3>
<p>I am going to use two global variables names and colors. Here are the functions needed for them.</p>
<p>I do not know why I set <code>Color = distinct int</code>, it is probably unnecessary and just complicates stuff.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">Color</span> = <span class="hljs-keyword">distinct</span> <span class="hljs-built_in">int</span> <span class="hljs-comment">## # why not use a distinct type to make our life a little bit messier?</span>
<span class="hljs-type">Names</span> = <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]
<span class="hljs-type">Colors</span> = <span class="hljs-type">OrderedTable</span>[<span class="hljs-built_in">string</span>, <span class="hljs-type">Color</span>]
<span class="hljs-keyword">var</span>
names: <span class="hljs-type">Names</span>
colors: <span class="hljs-type">Colors</span>
<span class="hljs-keyword">proc</span> addColor(name: <span class="hljs-built_in">string</span>) =
<span class="hljs-keyword">if</span> name <span class="hljs-keyword">not_in</span> names:
names.add name
<span class="hljs-keyword">let</span> i = names.len - <span class="hljs-number">1</span>
colors[name] = i.<span class="hljs-type">Color</span>
<span class="hljs-keyword">proc</span> `$`(c: <span class="hljs-type">Color</span>): <span class="hljs-built_in">string</span> =
names[c.<span class="hljs-built_in">int</span>] <span class="hljs-comment">## I take advantage that names is global</span>
<span class="hljs-keyword">proc</span> toColor(s: <span class="hljs-built_in">string</span>): <span class="hljs-type">Color</span> =
colors[s]
<span class="hljs-keyword">template</span> resetGlobals() =
<span class="hljs-comment">## to reset the globals (after solving for the example)</span>
names = @[]
colors = initOrderedTable[<span class="hljs-built_in">string</span>, <span class="hljs-type">Color</span>]()
addColor <span class="hljs-string">"shiny gold"</span> <span class="hljs-comment">## always will be the 0.Color</span>
resetGlobals
dump <span class="hljs-number">0.</span><span class="hljs-type">Color</span>
dump toColor(<span class="hljs-string">"shiny gold"</span>)
dump names
<span class="hljs-comment">## later on I will put Color in a table. You need this or you will get a rather cryptic error message</span>
<span class="hljs-keyword">proc</span> `==`(a, b: <span class="hljs-type">Color</span>): <span class="hljs-built_in">bool</span> {.borrow.}</code></pre>
<pre><samp>0.Color = shiny gold
toColor("shiny gold") = shiny gold
names = @["shiny gold"]</samp></pre>
<h3>What is a Rule and how to parse it</h3>
<p>note that parseRule does use the globals <code>names</code> and <code>colors</code>.</p>
<p>I did try first with npeg, but I am still missing stuff,
so for the moment I will keep to with manual parsing (<code>parseUntil</code> for the win!).</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">Rule</span> = <span class="hljs-keyword">tuple</span>[col: <span class="hljs-type">Color</span>, cons: <span class="hljs-type">Contents</span>]
<span class="hljs-type">Contents</span> = <span class="hljs-built_in">seq</span>[<span class="hljs-keyword">tuple</span>[qty: <span class="hljs-built_in">int</span>, col: <span class="hljs-type">Color</span>]]
<span class="hljs-keyword">proc</span> parseRule(text: <span class="hljs-built_in">string</span>): <span class="hljs-type">Rule</span> =
<span class="hljs-keyword">var</span>
col: <span class="hljs-built_in">string</span>
cons: <span class="hljs-built_in">string</span>
qty: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">let</span>
i = parseUntil(text, col, <span class="hljs-string">" bags"</span>)
j = <span class="hljs-string">" bags contain "</span>.len
<span class="hljs-keyword">discard</span> parseUntil(text, cons, <span class="hljs-string">"."</span>, start = i + j)
addColor col
<span class="hljs-literal">result</span>.col = col.toColor
<span class="hljs-keyword">for</span> con <span class="hljs-keyword">in</span> cons.split(<span class="hljs-string">", "</span>):
<span class="hljs-keyword">if</span> con == <span class="hljs-string">"no other bags"</span>:
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">let</span> i = parseInt(con, qty)
<span class="hljs-keyword">discard</span> parseUntil(con, col, <span class="hljs-string">" bag"</span>, start = i + <span class="hljs-number">1</span>)
addColor col
<span class="hljs-literal">result</span>.cons.add (qty, col.toColor)
<span class="hljs-keyword">echo</span> parseRule <span class="hljs-string">"light red bags contain 1 bright white bag, 2 muted yellow bags."</span>
<span class="hljs-keyword">echo</span> parseRule <span class="hljs-string">"bright white bags contain 1 shiny gold bag."</span>
<span class="hljs-keyword">echo</span> parseRule <span class="hljs-string">"faded blue bags contain no other bags."</span>
<span class="hljs-keyword">echo</span> names
<span class="hljs-keyword">echo</span> colors</code></pre>
<pre><samp>(col: light red, cons: @[(qty: 1, col: bright white), (qty: 2, col: muted yellow)])
(col: bright white, cons: @[(qty: 1, col: shiny gold)])
(col: faded blue, cons: @[])
@["shiny gold", "light red", "bright white", "muted yellow", "faded blue"]
{"shiny gold": shiny gold, "light red": light red, "bright white": bright white, "muted yellow": muted yellow, "faded blue": faded blue}</samp></pre>
<h3>reversing the rules</h3>
<p>while parsing the rules I will also create the mapping
from a color to its direct containers.</p>
<p>I do not know why I create a <code>PuzzleInput</code> type but I like how it looks in proc signatures.</p>
<p>The parsing is finally applied to the example.</p>
<p>I will reuse the variables <code>rs</code> and <code>dc</code>.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">PuzzleInput</span> = <span class="hljs-built_in">string</span>
<span class="hljs-type">Rules</span> = <span class="hljs-type">OrderedTable</span>[<span class="hljs-type">Color</span>, <span class="hljs-type">Contents</span>]
<span class="hljs-type">DirContainers</span> = <span class="hljs-type">OrderedTable</span>[<span class="hljs-type">Color</span>, <span class="hljs-built_in">seq</span>[<span class="hljs-type">Color</span>]]
<span class="hljs-keyword">proc</span> parse(p: <span class="hljs-type">PuzzleInput</span>): (<span class="hljs-type">Rules</span>, <span class="hljs-type">DirContainers</span>) =
<span class="hljs-keyword">var</span>
r: <span class="hljs-type">Rule</span>
rs = initOrderedTable[<span class="hljs-type">Color</span>, <span class="hljs-type">Contents</span>]()
dc = initOrderedTable[<span class="hljs-type">Color</span>, <span class="hljs-built_in">seq</span>[<span class="hljs-type">Color</span>]]()
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> p.splitLines:
r = parseRule line
rs[r.col] = r.cons
<span class="hljs-keyword">for</span> con <span class="hljs-keyword">in</span> r.cons:
<span class="hljs-keyword">if</span> con.col <span class="hljs-keyword">not_in</span> dc:
dc[con.col] = @[r.col]
<span class="hljs-keyword">elif</span> con.col <span class="hljs-keyword">not_in</span> dc[con.col]:
dc[con.col].add r.col
<span class="hljs-keyword">for</span> col <span class="hljs-keyword">in</span> colors.keys:
<span class="hljs-keyword">if</span> col.toColor <span class="hljs-keyword">not_in</span> dc:
dc[col.toColor] = newSeq[<span class="hljs-type">Color</span>]()
<span class="hljs-keyword">return</span> (rs, dc)
<span class="hljs-keyword">let</span> example = <span class="hljs-string">"""light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags."""</span>.<span class="hljs-type">PuzzleInput</span>
<span class="hljs-keyword">var</span> (rs, dc) = parse example
<span class="hljs-keyword">echo</span> <span class="hljs-string">"Rules:"</span>
<span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> rs:
<span class="hljs-keyword">echo</span> k, <span class="hljs-string">": "</span>, v
<span class="hljs-keyword">echo</span> <span class="hljs-string">"DirContainers:"</span>
<span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> dc:
<span class="hljs-keyword">echo</span> k, <span class="hljs-string">": "</span>, v</code></pre>
<pre><samp>Rules:
light red: @[(qty: 1, col: bright white), (qty: 2, col: muted yellow)]
dark orange: @[(qty: 3, col: bright white), (qty: 4, col: muted yellow)]
bright white: @[(qty: 1, col: shiny gold)]
muted yellow: @[(qty: 2, col: shiny gold), (qty: 9, col: faded blue)]
shiny gold: @[(qty: 1, col: dark olive), (qty: 2, col: vibrant plum)]
dark olive: @[(qty: 3, col: faded blue), (qty: 4, col: dotted black)]
vibrant plum: @[(qty: 5, col: faded blue), (qty: 6, col: dotted black)]
faded blue: @[]
dotted black: @[]
DirContainers:
bright white: @[light red, dark orange]
muted yellow: @[light red, dark orange]
shiny gold: @[bright white, muted yellow]
faded blue: @[muted yellow, dark olive, vibrant plum]
dark olive: @[shiny gold]
vibrant plum: @[shiny gold]
dotted black: @[dark olive, vibrant plum]
light red: @[]
dark orange: @[]</samp></pre>
<h3>Solve part 1</h3>
<p>with a short and careful recursion (note I keep variable <code>ac</code> in each recursive call).</p>
<p>output for real input will come later (so that I reset once the globals)</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">AllContainers</span> = <span class="hljs-type">OrderedSet</span>[<span class="hljs-type">Color</span>]
<span class="hljs-keyword">proc</span> allContainers(c: <span class="hljs-type">Color</span>; dc: <span class="hljs-type">DirContainers</span>; ac: <span class="hljs-keyword">var</span> <span class="hljs-type">AllContainers</span>) =
<span class="hljs-keyword">for</span> cx <span class="hljs-keyword">in</span> dc[c]:
<span class="hljs-keyword">if</span> cx <span class="hljs-keyword">in</span> ac:
ac.incl cx
<span class="hljs-keyword">else</span>:
ac.incl cx
allContainers(cx, dc, ac)
<span class="hljs-keyword">var</span> ac: <span class="hljs-type">AllContainers</span>
(<span class="hljs-number">0.</span><span class="hljs-type">Color</span>).allContainers(dc, ac)
<span class="hljs-keyword">echo</span> ac</code></pre>
<pre><samp>{bright white, light red, dark orange, muted yellow}</samp></pre>
<h3>Solve part2</h3>
<p>Showing first solving for the example.</p>
<p>I like the indentation trick I used to debug the solution, so I leave it visible.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">var</span> ind = <span class="hljs-number">0</span>
<span class="hljs-keyword">proc</span> reqBags(c: <span class="hljs-type">Color</span>; rs: <span class="hljs-type">Rules</span>): <span class="hljs-built_in">int</span> =
ind += <span class="hljs-number">2</span>
<span class="hljs-comment">## echo ("color: " & $c).indent(ind)</span>
<span class="hljs-comment">## echo ("contents: " & $(rs[c])).indent(ind)</span>
<span class="hljs-keyword">for</span> con <span class="hljs-keyword">in</span> rs[c]:
<span class="hljs-literal">result</span> += con.qty
<span class="hljs-literal">result</span> += con.qty * (reqBags(con.col, rs))
<span class="hljs-comment">## echo ("result: " & $result).indent(ind)</span>
ind -= <span class="hljs-number">2</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"reqBags(example): "</span>, (<span class="hljs-number">0.</span><span class="hljs-type">Color</span>).reqBags(rs)</code></pre>
<pre><samp>reqBags(example): 32</samp></pre>
<h3>Getting the stars</h3>
<p>Solutions for my puzzle input</p>
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> input = <span class="hljs-string">"2020/input07.txt"</span>.readFile.<span class="hljs-type">PuzzleInput</span>
resetGlobals
ac = initOrderedSet[<span class="hljs-type">Color</span>]() <span class="hljs-comment">## I need to reset this one too.</span>
(rs, dc) = parse input
(<span class="hljs-number">0.</span><span class="hljs-type">Color</span>).allContainers(dc, ac)
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part1: "</span>, ac.len
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part2: "</span>, (<span class="hljs-number">0.</span><span class="hljs-type">Color</span>).reqBags(rs)</code></pre>
<pre><samp>part1: 124
part2: 34862</samp></pre>
</main>
<footer>
<hr>
<div class="nb-box">
<span><span class="nb-small">made with <a href="https://pietroppeter.github.io/nimib/">nimib π³</a></span></span>
<span></span>
<span><button class="nb-small" id="show" onclick="toggleSourceDisplay()">Show Source</button></span>
</div>
</footer>
<section id="source">
<pre><code class="nim hljs"><span class="hljs-keyword">import</span> nimib, animu, sugar
nbInit
nbText: <span class="hljs-string">"## Day 7: Handy Haversacks"</span>
<span class="hljs-comment"># I should recover in nimib the simple toc example, it is indeed useful.</span>
nbText: <span class="hljs-string">"""### General strategy
The solution will be in 5 parts:
1. colors: I will use global variables
2. parse a single rule: manual parsing (no regex, no npeg - tried but did not succeed at first try)
3. reversing the rules: while parsing the rules I will also create the mapping
from a color to its direct containers.
4. solve part 1: recursive
5. solve part 2: recursive
"""</span>
nbText: <span class="hljs-string">"""### 1. Colors
I am going to use two global variables names and colors. Here are the functions needed for them.
I do not know why I set `Color = distinct int`, it is probably unnecessary and just complicates stuff.
"""</span>
nbCode:
<span class="hljs-keyword">type</span>
<span class="hljs-type">Color</span> = <span class="hljs-keyword">distinct</span> <span class="hljs-built_in">int</span> <span class="hljs-comment">### why not use a distinct type to make our life a little bit messier?</span>
<span class="hljs-type">Names</span> = <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]
<span class="hljs-type">Colors</span> = <span class="hljs-type">OrderedTable</span>[<span class="hljs-built_in">string</span>, <span class="hljs-type">Color</span>]
<span class="hljs-keyword">var</span>
names: <span class="hljs-type">Names</span>
colors: <span class="hljs-type">Colors</span>
<span class="hljs-keyword">proc</span> addColor(name: <span class="hljs-built_in">string</span>) =
<span class="hljs-keyword">if</span> name <span class="hljs-keyword">not_in</span> names:
names.add name
<span class="hljs-keyword">let</span> i = names.len - <span class="hljs-number">1</span>
colors[name] = i.<span class="hljs-type">Color</span>
<span class="hljs-keyword">proc</span> `$`(c: <span class="hljs-type">Color</span>): <span class="hljs-built_in">string</span> = names[c.<span class="hljs-built_in">int</span>] <span class="hljs-comment">## I take advantage that names is global</span>
<span class="hljs-keyword">proc</span> toColor(s: <span class="hljs-built_in">string</span>): <span class="hljs-type">Color</span> = colors[s]
<span class="hljs-keyword">template</span> resetGlobals =
<span class="hljs-comment">## to reset the globals (after solving for the example)</span>
names = @[]
colors = initOrderedTable[<span class="hljs-built_in">string</span>, <span class="hljs-type">Color</span>]()
addColor <span class="hljs-string">"shiny gold"</span> <span class="hljs-comment">## always will be the 0.Color</span>
resetGlobals
dump <span class="hljs-number">0.</span><span class="hljs-type">Color</span>
dump toColor(<span class="hljs-string">"shiny gold"</span>)
dump names
<span class="hljs-comment">## later on I will put Color in a table. You need this or you will get a rather cryptic error message</span>
<span class="hljs-keyword">proc</span> `==`(a,b: <span class="hljs-type">Color</span>): <span class="hljs-built_in">bool</span> {.borrow.}
nbText: <span class="hljs-string">"""### What is a Rule and how to parse it
note that parseRule does use the globals `names` and `colors`.
I did try first with npeg, but I am still missing stuff,
so for the moment I will keep to with manual parsing (`parseUntil` for the win!)."""</span>
nbCode:
<span class="hljs-keyword">type</span>
<span class="hljs-type">Rule</span> = <span class="hljs-keyword">tuple</span>[col: <span class="hljs-type">Color</span>, cons: <span class="hljs-type">Contents</span>]
<span class="hljs-type">Contents</span> = <span class="hljs-built_in">seq</span>[<span class="hljs-keyword">tuple</span>[qty: <span class="hljs-built_in">int</span>, col: <span class="hljs-type">Color</span>]]
<span class="hljs-keyword">proc</span> parseRule(text: <span class="hljs-built_in">string</span>): <span class="hljs-type">Rule</span> =
<span class="hljs-keyword">var</span>
col: <span class="hljs-built_in">string</span>
cons: <span class="hljs-built_in">string</span>
qty: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">let</span>
i = parseUntil(text, col, <span class="hljs-string">" bags"</span>)
j = <span class="hljs-string">" bags contain "</span>.len
<span class="hljs-keyword">discard</span> parseUntil(text, cons, <span class="hljs-string">"."</span>, start=i+j)
addColor col
<span class="hljs-literal">result</span>.col = col.toColor
<span class="hljs-keyword">for</span> con <span class="hljs-keyword">in</span> cons.split(<span class="hljs-string">", "</span>):
<span class="hljs-keyword">if</span> con == <span class="hljs-string">"no other bags"</span>:
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">let</span>
i = parseInt(con, qty)
<span class="hljs-keyword">discard</span> parseUntil(con, col, <span class="hljs-string">" bag"</span>, start=i+<span class="hljs-number">1</span>)
addColor col
<span class="hljs-literal">result</span>.cons.add (qty, col.toColor)
<span class="hljs-keyword">echo</span> parseRule <span class="hljs-string">"light red bags contain 1 bright white bag, 2 muted yellow bags."</span>
<span class="hljs-keyword">echo</span> parseRule <span class="hljs-string">"bright white bags contain 1 shiny gold bag."</span>
<span class="hljs-keyword">echo</span> parseRule <span class="hljs-string">"faded blue bags contain no other bags."</span>
<span class="hljs-keyword">echo</span> names
<span class="hljs-keyword">echo</span> colors
nbText: <span class="hljs-string">"""### reversing the rules
while parsing the rules I will also create the mapping
from a color to its direct containers.
I do not know why I create a `PuzzleInput` type but I like how it looks in proc signatures.
The parsing is finally applied to the example.
I will reuse the variables `rs` and `dc`."""</span>
nbCode:
<span class="hljs-keyword">type</span>
<span class="hljs-type">PuzzleInput</span> = <span class="hljs-built_in">string</span>
<span class="hljs-type">Rules</span> = <span class="hljs-type">OrderedTable</span>[<span class="hljs-type">Color</span>, <span class="hljs-type">Contents</span>]
<span class="hljs-type">DirContainers</span> = <span class="hljs-type">OrderedTable</span>[<span class="hljs-type">Color</span>, <span class="hljs-built_in">seq</span>[<span class="hljs-type">Color</span>]]
<span class="hljs-keyword">proc</span> parse(p: <span class="hljs-type">PuzzleInput</span>): (<span class="hljs-type">Rules</span>, <span class="hljs-type">DirContainers</span>) =
<span class="hljs-keyword">var</span>
r: <span class="hljs-type">Rule</span>
rs = initOrderedTable[<span class="hljs-type">Color</span>, <span class="hljs-type">Contents</span>]()
dc = initOrderedTable[<span class="hljs-type">Color</span>, <span class="hljs-built_in">seq</span>[<span class="hljs-type">Color</span>]]()
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> p.splitLines:
r = parseRule line
rs[r.col] = r.cons
<span class="hljs-keyword">for</span> con <span class="hljs-keyword">in</span> r.cons:
<span class="hljs-keyword">if</span> con.col <span class="hljs-keyword">not_in</span> dc:
dc[con.col] = @[r.col]
<span class="hljs-keyword">elif</span> con.col <span class="hljs-keyword">not_in</span> dc[con.col]:
dc[con.col].add r.col
<span class="hljs-keyword">for</span> col <span class="hljs-keyword">in</span> colors.keys:
<span class="hljs-keyword">if</span> col.toColor <span class="hljs-keyword">not_in</span> dc:
dc[col.toColor] = newSeq[<span class="hljs-type">Color</span>]()
<span class="hljs-keyword">return</span> (rs, dc)
<span class="hljs-keyword">let</span> example = <span class="hljs-string">"""light red bags contain 1 bright white bag, 2 muted yellow bags.
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags."""</span>.<span class="hljs-type">PuzzleInput</span>
<span class="hljs-keyword">var</span>
(rs, dc) = parse example
<span class="hljs-keyword">echo</span> <span class="hljs-string">"Rules:"</span>
<span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> rs:
<span class="hljs-keyword">echo</span> k, <span class="hljs-string">": "</span>, v
<span class="hljs-keyword">echo</span> <span class="hljs-string">"DirContainers:"</span>
<span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> dc:
<span class="hljs-keyword">echo</span> k, <span class="hljs-string">": "</span>, v
nbText: <span class="hljs-string">"""### Solve part 1
with a short and careful recursion (note I keep variable `ac` in each recursive call).
output for real input will come later (so that I reset once the globals)"""</span>
nbCode:
<span class="hljs-keyword">type</span> <span class="hljs-type">AllContainers</span> = <span class="hljs-type">OrderedSet</span>[<span class="hljs-type">Color</span>]
<span class="hljs-keyword">proc</span> allContainers(c: <span class="hljs-type">Color</span>, dc: <span class="hljs-type">DirContainers</span>, ac: <span class="hljs-keyword">var</span> <span class="hljs-type">AllContainers</span>) =
<span class="hljs-keyword">for</span> cx <span class="hljs-keyword">in</span> dc[c]:
<span class="hljs-keyword">if</span> cx <span class="hljs-keyword">in</span> ac: <span class="hljs-comment">## not sure if I need this, but for sure it is a protection against infinite recursion</span>
ac.incl cx
<span class="hljs-keyword">else</span>:
ac.incl cx
allContainers(cx, dc, ac)
<span class="hljs-keyword">var</span> ac: <span class="hljs-type">AllContainers</span>
(<span class="hljs-number">0.</span><span class="hljs-type">Color</span>).allContainers(dc, ac)
<span class="hljs-keyword">echo</span> ac
nbText: <span class="hljs-string">"""### Solve part2
Showing first solving for the example.
I like the indentation trick I used to debug the solution, so I leave it visible."""</span>
nbCode:
<span class="hljs-keyword">var</span> ind = <span class="hljs-number">0</span>
<span class="hljs-keyword">proc</span> reqBags(c: <span class="hljs-type">Color</span>, rs: <span class="hljs-type">Rules</span>): <span class="hljs-built_in">int</span> =
ind += <span class="hljs-number">2</span>
<span class="hljs-comment">##echo ("color: " & $c).indent(ind)</span>
<span class="hljs-comment">##echo ("contents: " & $(rs[c])).indent(ind)</span>
<span class="hljs-keyword">for</span> con <span class="hljs-keyword">in</span> rs[c]:
<span class="hljs-literal">result</span> += con.qty
<span class="hljs-literal">result</span> += con.qty*(reqBags(con.col, rs))
<span class="hljs-comment">##echo ("result: " & $result).indent(ind)</span>
ind -= <span class="hljs-number">2</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"reqBags(example): "</span>, (<span class="hljs-number">0.</span><span class="hljs-type">Color</span>).reqBags(rs)
nbText: <span class="hljs-string">"### Getting the stars</span><span class="hljs-meta">\n</span><span class="hljs-string">Solutions for my puzzle input"</span>
nbCode:
<span class="hljs-keyword">let</span> input = <span class="hljs-string">"2020/input07.txt"</span>.readFile.<span class="hljs-type">PuzzleInput</span>
resetGlobals
ac = initOrderedSet[<span class="hljs-type">Color</span>]() <span class="hljs-comment">## I need to reset this one too.</span>
(rs, dc) = parse input
(<span class="hljs-number">0.</span><span class="hljs-type">Color</span>).allContainers(dc, ac)
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part1: "</span>, ac.len
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part2: "</span>, (<span class="hljs-number">0.</span><span class="hljs-type">Color</span>).reqBags(rs)
<span class="hljs-comment">#gotTheStar</span>
<span class="hljs-comment">#gotTheStar</span>
nbSave</code></pre>
</section><script>
function toggleSourceDisplay() {
var btn = document.getElementById("show")
var source = document.getElementById("source");
if (btn.innerHTML=="Show Source") {
btn.innerHTML = "Hide Source";
source.style.display = "block";
} else {
btn.innerHTML = "Show Source";
source.style.display = "none";
}
}
</script></body>
</html>