-
Notifications
You must be signed in to change notification settings - Fork 1
/
day14.html
521 lines (490 loc) Β· 24.6 KB
/
day14.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2021/day14.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>2021/day14.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 14: <a href="https://adventofcode.com/2021/day/14">Extended Polymerization</a></h2>
<p>Today is a chemistry day!</p>
<h3>Part 1</h3>
<p>Parsing is straightforward</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span> <span class="hljs-type">Rules</span> = <span class="hljs-type">Table</span>[<span class="hljs-built_in">string</span>, <span class="hljs-built_in">char</span>]
<span class="hljs-keyword">func</span> parse(text: <span class="hljs-built_in">string</span>): <span class="hljs-type">Rules</span> =
<span class="hljs-literal">result</span> = initTable[<span class="hljs-built_in">string</span>, <span class="hljs-built_in">char</span>]()
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> text.splitLines:
<span class="hljs-literal">result</span>[line[<span class="hljs-number">0</span> .. <span class="hljs-number">1</span>]] = line.strip[^<span class="hljs-number">1</span>]
<span class="hljs-keyword">let</span>
testTemplate = <span class="hljs-string">"NNCB"</span>
puzzleTemplate = <span class="hljs-string">"VNVVKSNNFPBBBVSCVBBC"</span>
testRules = parse <span class="hljs-string">"""
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C"""</span>
puzzleRules = parse readFile(<span class="hljs-string">"2021/input14.txt"</span>)
dump testRules[<span class="hljs-string">"BB"</span>]
dump testRules.len
dump puzzleRules[<span class="hljs-string">"BB"</span>]
dump puzzleRules.len</code></pre>
<pre><samp>testRules["BB"] = N
testRules.len = 16
puzzleRules["BB"] = H
puzzleRules.len = 100
</samp></pre>
<p>Let's write the evolution function:</p>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> tick(rules: <span class="hljs-type">Rules</span>, polymer: <span class="hljs-built_in">string</span>): <span class="hljs-built_in">string</span> =
<span class="hljs-literal">result</span>.add polymer[<span class="hljs-number">0</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> ..< polymer.<span class="hljs-keyword">high</span>: <span class="hljs-comment"># until 1 less than highest index</span>
<span class="hljs-keyword">let</span> pair = polymer[i .. (i+<span class="hljs-number">1</span>)]
<span class="hljs-keyword">if</span> pair <span class="hljs-keyword">in</span> rules:
<span class="hljs-literal">result</span>.add rules[pair]
<span class="hljs-literal">result</span>.add polymer[i+<span class="hljs-number">1</span>]
<span class="hljs-keyword">func</span> tickN(rules: <span class="hljs-type">Rules</span>, polymer: <span class="hljs-built_in">string</span>, n=<span class="hljs-number">10</span>): <span class="hljs-built_in">string</span> =
<span class="hljs-literal">result</span> = polymer
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. n:
<span class="hljs-literal">result</span> = rules.tick <span class="hljs-literal">result</span>
debugEcho i, <span class="hljs-string">": "</span>, <span class="hljs-literal">result</span>.len
dump testRules.tick testTemplate
dump testRules.tickN(testTemplate, n=<span class="hljs-number">2</span>)
dump testRules.tickN(testTemplate, n=<span class="hljs-number">3</span>)
dump testRules.tickN(testTemplate, n=<span class="hljs-number">4</span>)</code></pre>
<pre><samp>testRules.tick testTemplate = NCNBCHB
1: 7
2: 13
testRules.tickN(testTemplate, n = 2) = NBCCNBBBCBHCB
1: 7
2: 13
3: 25
testRules.tickN(testTemplate, n = 3) = NBBBCNCCNBBNBNBBCHBHHBCHB
1: 7
2: 13
3: 25
4: 49
testRules.tickN(testTemplate, n = 4) = NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB
</samp></pre>
<p>and let's solve part 1</p>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> part1(rules: <span class="hljs-type">Rules</span>, polymer: <span class="hljs-built_in">string</span>, n=<span class="hljs-number">10</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">let</span> polymer = rules.tickN(polymer, n)
<span class="hljs-keyword">var</span> elementCountTable = polymer.toCountTable
elementCountTable.sort() <span class="hljs-comment"># default order is descending</span>
<span class="hljs-keyword">let</span>
keys = toSeq(elementCountTable.keys)
counts = toSeq(elementCountTable.values)
debugEcho <span class="hljs-string">"max: "</span>, keys[<span class="hljs-number">0</span>], <span class="hljs-string">" -> "</span>, counts[<span class="hljs-number">0</span>]
debugEcho <span class="hljs-string">"min: "</span>, keys[^<span class="hljs-number">1</span>], <span class="hljs-string">" -> "</span>, counts[^<span class="hljs-number">1</span>]
counts[<span class="hljs-number">0</span>] - counts[^<span class="hljs-number">1</span>]
dump part1(testRules, testTemplate)
dump part1(puzzleRules, puzzleTemplate)</code></pre>
<pre><samp>1: 7
2: 13
3: 25
4: 49
5: 97
6: 193
7: 385
8: 769
9: 1537
10: 3073
max: B -> 1749
min: H -> 161
part1(testRules, testTemplate) = 1588
1: 39
2: 77
3: 153
4: 305
5: 609
6: 1217
7: 2433
8: 4865
9: 9729
10: 19457
max: H -> 3618
min: C -> 997
part1(puzzleRules, puzzleTemplate) = 2621
</samp></pre>
<blockquote>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
</blockquote>
<p>all good except I did not read well and first implemented a function to count pairs
and not use the builtin for counting single elements (hence the <code>debugEchos</code>)</p>
<h2>Part 2</h2>
<p>ok, here clearly the brute force approach we used is not supposed to work and I am pretty sure
I am going to need the <code>countPairs</code> I just deleted...</p>
<pre><code class="nim hljs"><span class="hljs-comment"># had to try anyway, but after a few seconds I stopped</span>
<span class="hljs-comment">#dump part1(testRules, testTemplate, n=40)</span>
<span class="hljs-comment">#dump part1(puzzleRules, puzzleTemplate, n=40)</span>
<span class="hljs-keyword">discard</span> <span class="hljs-comment"># cannot have a code block with only comments!</span></code></pre>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span> <span class="hljs-type">CountPairs</span> = <span class="hljs-type">CountTable</span>[<span class="hljs-built_in">string</span>]
<span class="hljs-keyword">func</span> getCountPairs(polymer: <span class="hljs-built_in">string</span>): <span class="hljs-type">CountPairs</span> =
<span class="hljs-literal">result</span> = initCountTable[<span class="hljs-built_in">string</span>]()
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> ..< polymer.<span class="hljs-keyword">high</span>:
<span class="hljs-keyword">let</span> pair = polymer[i .. (i+<span class="hljs-number">1</span>)]
<span class="hljs-literal">result</span>.inc pair
<span class="hljs-keyword">func</span> tick(rules: <span class="hljs-type">Rules</span>, countPairs: <span class="hljs-type">CountPairs</span>): <span class="hljs-type">CountPairs</span> =
<span class="hljs-literal">result</span> = initCountTable[<span class="hljs-built_in">string</span>]()
<span class="hljs-keyword">for</span> pair <span class="hljs-keyword">in</span> countPairs.keys:
<span class="hljs-keyword">if</span> pair <span class="hljs-keyword">in</span> rules:
<span class="hljs-keyword">let</span>
count = countPairs[pair]
newPairLeft = pair[<span class="hljs-number">0</span>] & rules[pair]
newPairRight = rules[pair] & pair[<span class="hljs-number">1</span>]
<span class="hljs-literal">result</span>.inc(newPairLeft, count)
<span class="hljs-literal">result</span>.inc(newPairRight, count)
<span class="hljs-keyword">func</span> tickN(rules: <span class="hljs-type">Rules</span>, countPairs: <span class="hljs-type">CountPairs</span>, n=<span class="hljs-number">40</span>): <span class="hljs-type">CountPairs</span> =
<span class="hljs-literal">result</span> = countPairs
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. n:
<span class="hljs-literal">result</span> = rules.tick <span class="hljs-literal">result</span>
debugEcho i, <span class="hljs-string">": "</span>, <span class="hljs-literal">result</span>.len <span class="hljs-comment"># how many pairs</span>
<span class="hljs-keyword">func</span> part2(rules: <span class="hljs-type">Rules</span>, polymer: <span class="hljs-built_in">string</span>, n=<span class="hljs-number">40</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span> countPairs = polymer.getCountPairs
countPairs = rules.tickN(countPairs, n)
<span class="hljs-keyword">var</span> elementCountTable = initCountTable[<span class="hljs-built_in">char</span>]()
<span class="hljs-keyword">for</span> pair, count <span class="hljs-keyword">in</span> countPairs:
elementCountTable.inc(pair[<span class="hljs-number">1</span>], val=count)
elementCountTable.inc(polymer[<span class="hljs-number">0</span>]) <span class="hljs-comment"># need to count again first element</span>
elementCountTable.sort() <span class="hljs-comment"># default order is descending</span>
<span class="hljs-keyword">let</span>
keys = toSeq(elementCountTable.keys)
counts = toSeq(elementCountTable.values)
debugEcho <span class="hljs-string">"max: "</span>, keys[<span class="hljs-number">0</span>], <span class="hljs-string">" -> "</span>, counts[<span class="hljs-number">0</span>]
debugEcho <span class="hljs-string">"min: "</span>, keys[^<span class="hljs-number">1</span>], <span class="hljs-string">" -> "</span>, counts[^<span class="hljs-number">1</span>]
counts[<span class="hljs-number">0</span>] - counts[^<span class="hljs-number">1</span>]
dump part2(testRules, testTemplate, n=<span class="hljs-number">10</span>)
dump part2(puzzleRules, puzzleTemplate, n=<span class="hljs-number">10</span>)</code></pre>
<pre><samp>1: 6
2: 8
3: 11
4: 13
5: 14
6: 15
7: 15
8: 15
9: 15
10: 15
max: B -> 1749
min: H -> 161
part2(testRules, testTemplate, n = 10) = 1588
1: 30
2: 47
3: 62
4: 73
5: 77
6: 78
7: 78
8: 78
9: 78
10: 78
max: H -> 3618
min: C -> 997
part2(puzzleRules, puzzleTemplate, n = 10) = 2621
</samp></pre>
<p>ok, checks out with part 1</p>
<pre><code class="nim hljs">dump part2(testRules, testTemplate, n=<span class="hljs-number">40</span>)
dump part2(puzzleRules, puzzleTemplate, n=<span class="hljs-number">40</span>)</code></pre>
<pre><samp>1: 6
2: 8
3: 11
4: 13
5: 14
6: 15
7: 15
8: 15
9: 15
10: 15
11: 15
12: 15
13: 15
14: 15
15: 15
16: 15
17: 15
18: 15
19: 15
20: 15
21: 15
22: 15
23: 15
24: 15
25: 15
26: 15
27: 15
28: 15
29: 15
30: 15
31: 15
32: 15
33: 15
34: 15
35: 15
36: 15
37: 15
38: 15
39: 15
40: 15
max: B -> 2192039569602
min: H -> 3849876073
part2(testRules, testTemplate, n = 40) = 2188189693529
1: 30
2: 47
3: 62
4: 73
5: 77
6: 78
7: 78
8: 78
9: 78
10: 78
11: 78
12: 78
13: 78
14: 78
15: 78
16: 78
17: 78
18: 78
19: 78
20: 78
21: 78
22: 78
23: 78
24: 78
25: 78
26: 78
27: 78
28: 78
29: 78
30: 78
31: 78
32: 78
33: 78
34: 78
35: 78
36: 78
37: 78
38: 78
39: 78
40: 78
max: H -> 3918098460190
min: C -> 1074264218824
part2(puzzleRules, puzzleTemplate, n = 40) = 2843834241366
</samp></pre>
<blockquote>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
</blockquote>
<p>surprised I got the second part at first try (had to think a bit to get it correct)!</p>
</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> animu, nimib
nbInit(theme=useAdventOfNim)
nbText: <span class="hljs-string">"""## Day 14: [Extended Polymerization](https://adventofcode.com/2021/day/14)
Today is a chemistry day!
### Part 1
Parsing is straightforward
"""</span>
nbCode:
<span class="hljs-keyword">type</span> <span class="hljs-type">Rules</span> = <span class="hljs-type">Table</span>[<span class="hljs-built_in">string</span>, <span class="hljs-built_in">char</span>]
<span class="hljs-keyword">func</span> parse(text: <span class="hljs-built_in">string</span>): <span class="hljs-type">Rules</span> =
<span class="hljs-literal">result</span> = initTable[<span class="hljs-built_in">string</span>, <span class="hljs-built_in">char</span>]()
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> text.splitLines:
<span class="hljs-literal">result</span>[line[<span class="hljs-number">0</span> .. <span class="hljs-number">1</span>]] = line.strip[^<span class="hljs-number">1</span>]
<span class="hljs-keyword">let</span>
testTemplate = <span class="hljs-string">"NNCB"</span>
puzzleTemplate = <span class="hljs-string">"VNVVKSNNFPBBBVSCVBBC"</span>
testRules = parse <span class="hljs-string">"""
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C"""</span>
puzzleRules = parse readFile(<span class="hljs-string">"2021/input14.txt"</span>)
dump testRules[<span class="hljs-string">"BB"</span>]
dump testRules.len
dump puzzleRules[<span class="hljs-string">"BB"</span>]
dump puzzleRules.len
nbText: <span class="hljs-string">"Let's write the evolution function:"</span>
nbCode:
<span class="hljs-keyword">func</span> tick(rules: <span class="hljs-type">Rules</span>, polymer: <span class="hljs-built_in">string</span>): <span class="hljs-built_in">string</span> =
<span class="hljs-literal">result</span>.add polymer[<span class="hljs-number">0</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> ..< polymer.<span class="hljs-keyword">high</span>: <span class="hljs-comment"># until 1 less than highest index</span>
<span class="hljs-keyword">let</span> pair = polymer[i .. (i+<span class="hljs-number">1</span>)]
<span class="hljs-keyword">if</span> pair <span class="hljs-keyword">in</span> rules:
<span class="hljs-literal">result</span>.add rules[pair]
<span class="hljs-literal">result</span>.add polymer[i+<span class="hljs-number">1</span>]
<span class="hljs-keyword">func</span> tickN(rules: <span class="hljs-type">Rules</span>, polymer: <span class="hljs-built_in">string</span>, n=<span class="hljs-number">10</span>): <span class="hljs-built_in">string</span> =
<span class="hljs-literal">result</span> = polymer
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. n:
<span class="hljs-literal">result</span> = rules.tick <span class="hljs-literal">result</span>
debugEcho i, <span class="hljs-string">": "</span>, <span class="hljs-literal">result</span>.len
dump testRules.tick testTemplate
dump testRules.tickN(testTemplate, n=<span class="hljs-number">2</span>)
dump testRules.tickN(testTemplate, n=<span class="hljs-number">3</span>)
dump testRules.tickN(testTemplate, n=<span class="hljs-number">4</span>)
nbText: <span class="hljs-string">"and let's solve part 1"</span>
nbCode:
<span class="hljs-keyword">func</span> part1(rules: <span class="hljs-type">Rules</span>, polymer: <span class="hljs-built_in">string</span>, n=<span class="hljs-number">10</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">let</span> polymer = rules.tickN(polymer, n)
<span class="hljs-keyword">var</span> elementCountTable = polymer.toCountTable
elementCountTable.sort() <span class="hljs-comment"># default order is descending</span>
<span class="hljs-keyword">let</span>
keys = toSeq(elementCountTable.keys)
counts = toSeq(elementCountTable.values)
debugEcho <span class="hljs-string">"max: "</span>, keys[<span class="hljs-number">0</span>], <span class="hljs-string">" -> "</span>, counts[<span class="hljs-number">0</span>]
debugEcho <span class="hljs-string">"min: "</span>, keys[^<span class="hljs-number">1</span>], <span class="hljs-string">" -> "</span>, counts[^<span class="hljs-number">1</span>]
counts[<span class="hljs-number">0</span>] - counts[^<span class="hljs-number">1</span>]
dump part1(testRules, testTemplate)
dump part1(puzzleRules, puzzleTemplate)
gotTheStar
nbText: <span class="hljs-string">"""all good except I did not read well and first implemented a function to count pairs
and not use the builtin for counting single elements (hence the `debugEchos`)
## Part 2
ok, here clearly the brute force approach we used is not supposed to work and I am pretty sure
I am going to need the `countPairs` I just deleted...
"""</span>
nbCode:
<span class="hljs-comment"># had to try anyway, but after a few seconds I stopped</span>
<span class="hljs-comment">#dump part1(testRules, testTemplate, n=40)</span>
<span class="hljs-comment">#dump part1(puzzleRules, puzzleTemplate, n=40)</span>
<span class="hljs-keyword">discard</span> <span class="hljs-comment"># cannot have a code block with only comments!</span>
nbCode:
<span class="hljs-keyword">type</span> <span class="hljs-type">CountPairs</span> = <span class="hljs-type">CountTable</span>[<span class="hljs-built_in">string</span>]
<span class="hljs-keyword">func</span> getCountPairs(polymer: <span class="hljs-built_in">string</span>): <span class="hljs-type">CountPairs</span> =
<span class="hljs-literal">result</span> = initCountTable[<span class="hljs-built_in">string</span>]()
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> ..< polymer.<span class="hljs-keyword">high</span>:
<span class="hljs-keyword">let</span> pair = polymer[i .. (i+<span class="hljs-number">1</span>)]
<span class="hljs-literal">result</span>.inc pair
<span class="hljs-keyword">func</span> tick(rules: <span class="hljs-type">Rules</span>, countPairs: <span class="hljs-type">CountPairs</span>): <span class="hljs-type">CountPairs</span> =
<span class="hljs-literal">result</span> = initCountTable[<span class="hljs-built_in">string</span>]()
<span class="hljs-keyword">for</span> pair <span class="hljs-keyword">in</span> countPairs.keys:
<span class="hljs-keyword">if</span> pair <span class="hljs-keyword">in</span> rules:
<span class="hljs-keyword">let</span>
count = countPairs[pair]
newPairLeft = pair[<span class="hljs-number">0</span>] & rules[pair]
newPairRight = rules[pair] & pair[<span class="hljs-number">1</span>]
<span class="hljs-literal">result</span>.inc(newPairLeft, count)
<span class="hljs-literal">result</span>.inc(newPairRight, count)
<span class="hljs-keyword">func</span> tickN(rules: <span class="hljs-type">Rules</span>, countPairs: <span class="hljs-type">CountPairs</span>, n=<span class="hljs-number">40</span>): <span class="hljs-type">CountPairs</span> =
<span class="hljs-literal">result</span> = countPairs
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. n:
<span class="hljs-literal">result</span> = rules.tick <span class="hljs-literal">result</span>
debugEcho i, <span class="hljs-string">": "</span>, <span class="hljs-literal">result</span>.len <span class="hljs-comment"># how many pairs</span>
<span class="hljs-keyword">func</span> part2(rules: <span class="hljs-type">Rules</span>, polymer: <span class="hljs-built_in">string</span>, n=<span class="hljs-number">40</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span> countPairs = polymer.getCountPairs
countPairs = rules.tickN(countPairs, n)
<span class="hljs-keyword">var</span> elementCountTable = initCountTable[<span class="hljs-built_in">char</span>]()
<span class="hljs-keyword">for</span> pair, count <span class="hljs-keyword">in</span> countPairs:
elementCountTable.inc(pair[<span class="hljs-number">1</span>], val=count)
elementCountTable.inc(polymer[<span class="hljs-number">0</span>]) <span class="hljs-comment"># need to count again first element</span>
elementCountTable.sort() <span class="hljs-comment"># default order is descending</span>
<span class="hljs-keyword">let</span>
keys = toSeq(elementCountTable.keys)
counts = toSeq(elementCountTable.values)
debugEcho <span class="hljs-string">"max: "</span>, keys[<span class="hljs-number">0</span>], <span class="hljs-string">" -> "</span>, counts[<span class="hljs-number">0</span>]
debugEcho <span class="hljs-string">"min: "</span>, keys[^<span class="hljs-number">1</span>], <span class="hljs-string">" -> "</span>, counts[^<span class="hljs-number">1</span>]
counts[<span class="hljs-number">0</span>] - counts[^<span class="hljs-number">1</span>]
dump part2(testRules, testTemplate, n=<span class="hljs-number">10</span>)
dump part2(puzzleRules, puzzleTemplate, n=<span class="hljs-number">10</span>)
nbText: <span class="hljs-string">"ok, checks out with part 1"</span>
nbCode:
dump part2(testRules, testTemplate, n=<span class="hljs-number">40</span>)
dump part2(puzzleRules, puzzleTemplate, n=<span class="hljs-number">40</span>)
gotTheStar
nbText: <span class="hljs-string">"surprised I got the second part at first try (had to think a bit to get it correct)!"</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>