-
Notifications
You must be signed in to change notification settings - Fork 1
/
day03.html
796 lines (743 loc) · 57 KB
/
day03.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2021/day03.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/day03.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 3: <a href="https://adventofcode.com/2021/day/3">Binary Diagnostic</a> 👯🎛️</h2>
<p>I had to catch a plane this morning and the puzzle unlocked
while getting onto the plane. Luckily I was able to solve part1 in the few
minutes from the moment I sat on the plane to the moment they asked us
to turn off the devices, so I could get also the text for part2 and had
plenty of time during flight to solve it.</p>
<p>Today is also the day in which I finally realize a dream I have been having
since last edition of Advent Of Code: I want to be able to <em>listen to a puzzle input</em>
and I want to do it using the genuinely bizarre and bizarrely ingenious library <a href="https://github.com/paranim/paramidi">paramidi</a>
by one of my favorite Nim authors:
<a href="https://github.com/oakes">oakes</a> (who is also responsible for <a href="https://github.com/oakes/vim_cubed">vim_cubed</a>,
which I think is the most starred Nim repository after Nim compiler).</p>
<p>In order to do that, I accidentally released <a href="https://pietroppeter.github.io/paramidib/">paramidib</a> (not a typo) library,
which is just two templates to have better access to paramidi through nimib.
You can now follow along reading my rather dull solution for today
or head directly for <a href="#whale_music">Part 3: Whale Music 🐳🎶</a>.</p>
<p>Enjoy <a href="https://forum.nim-lang.org/t/8657">Advent of Nim</a>! 🎄👑</p>
<h3>Part 1</h3>
<blockquote>
<p>The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions of the submarine. The first parameter to check is the <em>power consumption</em>.</p>
<p>You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the <em>gamma rate</em> and the <em>epsilon rate</em>). The power consumption can then be found by multiplying the gamma rate by the epsilon rate.</p>
<p>Each bit in the gamma rate can be determined by finding the <em>most common bit in the corresponding position</em> of all numbers in the diagnostic report. For example, given the following diagnostic report:</p>
</blockquote>
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> testInput = <span class="hljs-string">"""
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010"""</span></code></pre>
<blockquote>
<p>Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1.</p>
<p>The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0.</p>
<p>The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110.</p>
<p>So, the gamma rate is the binary number 10110, or <em>22</em> in decimal.</p>
<p>The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001, or <em>9</em> in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, <em>198</em>.</p>
<p>Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. <em>What is the power consumption of the submarine?</em></p>
</blockquote>
<pre><code class="nim hljs"><span class="hljs-keyword">let</span>
testNums = testInput.splitLines.toSeq
puzzleNums = <span class="hljs-string">"2021/input03.txt"</span>.lines.toSeq</code></pre>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> getGamma(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span>
gamma = <span class="hljs-string">""</span>
countOnes: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. nums[<span class="hljs-number">0</span>].<span class="hljs-keyword">high</span>:
countOnes = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> nums:
<span class="hljs-keyword">if</span> num[i] == <span class="hljs-string">'1'</span>:
inc countOnes
<span class="hljs-keyword">if</span> countOnes >= nums.len <span class="hljs-keyword">div</span> <span class="hljs-number">2</span>:
gamma.add <span class="hljs-string">'1'</span>
<span class="hljs-keyword">else</span>:
gamma.add <span class="hljs-string">'0'</span>
<span class="hljs-literal">result</span> = parseBinInt gamma
dump testNums.getGamma
dump puzzleNums.getGamma</code></pre>
<pre><samp>testNums.getGamma = 22
puzzleNums.getGamma = 2502
</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>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> part1(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">let</span>
gamma = getGamma(nums)
<span class="hljs-comment"># 2^5(=32) - 22 - 1 -> 9 </span>
epsilon = <span class="hljs-number">2</span>^len(nums[<span class="hljs-number">0</span>]) - gamma
<span class="hljs-keyword">return</span> gamma*epsilon
dump part1 testNums
dump part1 puzzleNums</code></pre>
<pre><samp>part1 testNums = 220
part1 puzzleNums = 3988188
</samp></pre>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> lifeSupport(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>], oxy=<span class="hljs-literal">true</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span>
nums = nums <span class="hljs-comment"># idiomatic</span>
i = <span class="hljs-number">0</span>
zeros, ones: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]
<span class="hljs-keyword">while</span> nums.len > <span class="hljs-number">1</span>:
zeros = @[] <span class="hljs-comment"># zeros, ones = @[] does not work</span>
ones = @[]
<span class="hljs-keyword">for</span> j, num <span class="hljs-keyword">in</span> nums:
<span class="hljs-keyword">if</span> num[i] == <span class="hljs-string">'1'</span>:
ones.add j
<span class="hljs-keyword">else</span>:
zeros.add j
<span class="hljs-comment">#dump ones</span>
<span class="hljs-comment">#dump zeros</span>
<span class="hljs-keyword">if</span> oxy:
<span class="hljs-keyword">if</span> ones.len >= zeros.len:
nums = collect:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> ones:
nums[j]
<span class="hljs-keyword">else</span>:
nums = collect:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> zeros:
nums[j]
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">if</span> zeros.len <= ones.len:
nums = collect:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> zeros:
nums[j]
<span class="hljs-keyword">else</span>:
nums = collect:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> ones:
nums[j]
inc i
<span class="hljs-comment">#debugEcho nums</span>
<span class="hljs-keyword">assert</span> nums.len == <span class="hljs-number">1</span>
<span class="hljs-keyword">return</span> parseBinInt(nums[<span class="hljs-number">0</span>])
<span class="hljs-keyword">func</span> part2(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
lifeSupport(nums)*lifeSupport(nums, <span class="hljs-literal">false</span>)
dump part2 testNums
dump part2 puzzleNums</code></pre>
<pre><samp>part2 testNums = 230
part2 puzzleNums = 2555739
</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>
<h3>Part 3: <a name="whale_music">Whale Music 🐳🎶</a></h3>
<blockquote>
<p>on top of the odd creaking noises that the submarine has been making in its descent,
you start to hear a more pleasant melody that vibrates all through the hull.
By looking outside the small circular windows you realize that you are surrounded
by <a href="https://www.nationalgeographic.com/magazine/graphics/here-is-how-whales-sing-from-yaps-to-fin-slaps-feature">singing whales</a>!</p>
<p>Having already completed your star collection for the day,
you decide to dedicate the rest of the day (and night) to learn more about this songs.</p>
<p>Luckily, the <em>library of the submarnim</em> has a small booklet by one mysterious
<a href="https://sekao.net">Kaz Cohaes</a> entitled
<a href="https://it.wikipedia.org/wiki/Paralipomeni_della_Batracomiomachia">paralipomena of phalenotragoùdia</a>
that promises through the <a href="https://github.com/oakes/ansiwave">lost art of midi</a> to
allow you to be able to communicate with the singing whales!</p>
<p>You are definitely in luck today, since it appears that the key ingredient to make
such a communication possible is a <em>diagnostic report</em> like the one produced by your submarine!
You also happen to have the
latest version of terminal <em>N.I.M.I.B.</em> (Natural Inducer of Music for Interesting Balenas)
which you will use to solve the exercises proposed by the booklet.</p>
<p>The first chapter of PoP (short for Paralipomena of phalenotragoùdia) starts with the basic
assumption of whale singing. Communication happen through <em>musical scales</em> and
the utmost expressiveness is reached through <em>alternating moments of music and moments of silence</em>.
Your first exercise is to translate your test diagnostic report into music.</p>
<p><em>Each line of the report is the repetition of the same scale, with the zeros representing
moments of silence, while when a <em>1</em> is present the note must be emitted</em>.</p>
</blockquote>
<p>We will use library <a href="https://github.com/paranim/paramidi">paramidi</a> to produce the music, but we import it as <a href="https://github.com/pietroppeter/paramidib">paramidib</a> (not a typo).
This is a convenience thin layer that exposes two templates to save a paramidi score
to a wave file and then to expose such a wave file through an html audio control.</p>
<p>Since the test diagnostic report is made of strings of 5 digits we will use a
<a href="https://en.wikipedia.org/wiki/Pentatonic_scale">pentatonic scale</a>.
The primary way to write a paramidi music score is through a bizarre data structure
which is a rather freely constructed hierarchy of tuples.
If you find an instrument in the tuple, it means that what follows will be played
with that instrument, a tempo annotation sets the tempo, a ratio of integers sets the
length of next note, then finally come the notes which have the usual syntax through
letter of the alphabet (there are ways also to change octave and to write chromatic notes).
A letter <em>r</em> denotes a moment of rest (silence).</p>
<p>For the first exercise we will encode manually the score.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">import</span> paramidib
saveMusic(<span class="hljs-string">"2021/day03test.wav"</span>):
(piano,
(tempo: <span class="hljs-number">109</span>), <span class="hljs-comment"># allegretto</span>
<span class="hljs-number">1</span>/<span class="hljs-number">5</span>,
<span class="hljs-comment"># A C D E G </span>
<span class="hljs-comment"># 0 0 1 0 0</span>
r,r,d,r,r,
<span class="hljs-comment"># 1 1 1 1 0</span>
a,c,d,e,r,
<span class="hljs-comment"># 1 0 1 1 0</span>
a,r,d,e,r,
<span class="hljs-comment"># 1 0 1 1 1</span>
a,r,d,e,g,
<span class="hljs-comment"># 1 0 1 0 1</span>
a,r,d,r,g,
<span class="hljs-comment"># 0 1 1 1 1</span>
r,c,d,e,g,
<span class="hljs-comment"># 0 0 1 1 1</span>
r,r,d,e,g,
<span class="hljs-comment"># 1 1 1 0 0</span>
a,c,d,r,r,
<span class="hljs-comment"># 1 0 0 0 0</span>
a,r,r,r,r,
<span class="hljs-comment"># 1 1 0 0 1</span>
a,c,r,r,g,
<span class="hljs-comment"># 0 0 0 1 0</span>
r,r,r,e,r,
<span class="hljs-comment"># 0 1 0 1 0</span>
r,c,r,e,r
)</code></pre>
<audio controls>
<source src="../2021/day03test.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
<p>Good job on producing the first piece of music!</p>
<blockquote>
<p>The second chapter of PoP tells you that longer sequence of digits
may represent <a href="https://en.wiktionary.org/wiki/arpeggio">arpeggios</a> of scales
going up and down</p>
<p>The third chapter tells you that whale are apparently fond a <a href="https://en.wikipedia.org/wiki/Bebop">bop</a>
and they like the color <a href="https://en.wikipedia.org/wiki/Blue">blue</a>.</p>
</blockquote>
<p>Since the puzzle input is made of sequences of 12 digits, we will use
a <a href="https://en.wikipedia.org/wiki/Blues_scale">blues scale</a> which is made of 6 notes
and we will go up and down the scale with an arpeggio.</p>
<p>Here is how this scale sound:</p>
<pre><code class="nim hljs"><span class="hljs-comment"># blues scale: A C D Eb E G</span>
saveMusic(<span class="hljs-string">"2021/blues_scale.wav"</span>):
(altosax,
(tempo: <span class="hljs-number">234</span>), <span class="hljs-number">2</span>/<span class="hljs-number">12</span>,
-a, c, d, e♭, e, g,
a, g, e, e♭, d, c,
-a, c, d, e♭, e, g,
a, g, e, e♭, d, c,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, -a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, -a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c,
)</code></pre>
<audio controls>
<source src="../2021/blues_scale.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
<p>Note that:</p>
<ul>
<li>we pass from a straight ryhthm to <a href="https://en.wikipedia.org/wiki/Swing_(jazz_performance_style)">triplet swing</a></li>
<li>the instrument is a sax alto and the tempo is the same of Charlie Bird's <a href="https://getsongbpm.com/song/ornithology/MjKLP5">Ornithology</a> (<em>prestissimo</em>)</li>
<li><code>-a</code> is used for a <code>a</code> one octave lower</li>
<li><code>dx</code> is actually substituted through a <a href="https://nim-lang.org/docs/filters.html">source code filter</a> to <code>dx</code> which represents <code>d#</code></li>
</ul>
<p>Now let's try to see how this would look with the moments of silence on zeros.
We take the forst two lines of puzzle input to test how it sounds:</p>
<pre><code class="nim hljs"><span class="hljs-keyword">echo</span> puzzleNums[<span class="hljs-number">0</span> .. <span class="hljs-number">1</span>]</code></pre>
<pre><samp>@["111111010011", "110011001100"]
</samp></pre>
<pre><code class="nim hljs">saveMusic(<span class="hljs-string">"2021/day03puzzle_head.wav"</span>):
(altosax,
(tempo: <span class="hljs-number">234</span>),
<span class="hljs-comment"># 1 1 1 1 1 1</span>
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, -a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g,
<span class="hljs-comment"># 0 1 0 0 1 1</span>
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, r , <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c,
<span class="hljs-comment"># 1 1 0 0 1 1</span>
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, -a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, r , <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g,
<span class="hljs-comment"># 0 0 1 1 0 0</span>
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, r,
)</code></pre>
<audio controls>
<source src="../2021/day03puzzle_head.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
<p>A tuple is an immutable data structure and since Nim is strongly typed it
is not the most flexible tool to write a score at runtime.
Luckily paramidi offers another way to write scores using <code>json</code> variant objects.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">import</span> json
<span class="hljs-keyword">let</span> puzzleHead =
%*[<span class="hljs-string">"alto-sax"</span>, {<span class="hljs-string">"tempo"</span>: <span class="hljs-number">234</span>},
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"a-"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"c"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"d"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"d#"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"e"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"g"</span>,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"g"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span> , <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"d"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"c"</span>,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"a-"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"c"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span> , <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"e"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"g"</span>,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"e"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"d#"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>,
]
saveMusic(<span class="hljs-string">"2021/day03puzzle_head_alt.wav"</span>, puzzle_head)</code></pre>
<audio controls>
<source src="../2021/day03puzzle_head_alt.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
<p>Note that:</p>
<ul>
<li>instead of <code>altosax</code> we need to use <code>alto-sax</code></li>
<li>instead of <code>-a</code> we use <code>a-</code></li>
<li>instead of <code>dx</code> we use <code>d#</code>
In fact all this alternative syntax is the most natural one since it derives</li>
</ul>
<p>from the C library that paramidi used to produce the sound.
See the list of available constants <a href="https://github.com/paranim/paramidi/blob/master/src/paramidi/constants.nim">here</a>.</p>
<p>Now we are ready to create the score starting from a list of inputs in an automatic way:</p>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> getScore(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>], denominator=<span class="hljs-number">12</span>): <span class="hljs-type">JsonNode</span> =
<span class="hljs-keyword">const</span> arpeggio = @[<span class="hljs-string">"a-"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"d#"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"g"</span>, <span class="hljs-string">"a"</span>, <span class="hljs-string">"g"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"d#"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"c"</span>]
<span class="hljs-literal">result</span> = %*[<span class="hljs-string">"alto-sax"</span>, {<span class="hljs-string">"tempo"</span>: <span class="hljs-number">234</span>}]
<span class="hljs-keyword">var</span>
beat = <span class="hljs-literal">true</span>
i = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> nums:
<span class="hljs-keyword">for</span> digit <span class="hljs-keyword">in</span> num:
<span class="hljs-keyword">if</span> beat:
<span class="hljs-literal">result</span>.add %(<span class="hljs-number">2</span>/denominator)
<span class="hljs-keyword">else</span>:
<span class="hljs-literal">result</span>.add %(<span class="hljs-number">1</span>/denominator)
<span class="hljs-keyword">if</span> digit == <span class="hljs-string">'1'</span>:
<span class="hljs-literal">result</span>.add %(arpeggio[i <span class="hljs-keyword">mod</span> <span class="hljs-number">12</span>])
<span class="hljs-keyword">else</span>:
<span class="hljs-literal">result</span>.add %<span class="hljs-string">"r"</span>
inc i
beat = <span class="hljs-keyword">not</span> beat
saveMusic(<span class="hljs-string">"2021/day03puzzle_head_longer.wav"</span>, getScore(puzzleNums[<span class="hljs-number">0</span> ..< <span class="hljs-number">10</span>]))
saveMusic(<span class="hljs-string">"2021/day03puzzle_head_faster.wav"</span>, getScore(puzzleNums[<span class="hljs-number">0</span> ..< <span class="hljs-number">20</span>], denominator=<span class="hljs-number">24</span>))</code></pre>
<audio controls>
<source src="../2021/day03puzzle_head_longer.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
<audio controls>
<source src="../2021/day03puzzle_head_faster.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
<p>We can also decide that we want to hear stuff at double the velocity.</p>
<p>Finally, I cannot put the full song since it is too big for github, but here
is a 10 minute excerpt (full song is around 25 minutes):</p>
<pre><code class="nim hljs">saveMusic(<span class="hljs-string">"2021/day03puzzle.wav"</span>, getScore(puzzleNums[<span class="hljs-number">0</span> .. <span class="hljs-number">400</span>]))
saveMusic(<span class="hljs-string">"2021/day03puzzle_faster.wav"</span>, getScore(puzzleNums[<span class="hljs-number">0</span> .. <span class="hljs-number">400</span>], denominator=<span class="hljs-number">24</span>))</code></pre>
<audio controls>
<source src="../2021/day03puzzle.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
<audio controls>
<source src="../2021/day03puzzle_faster.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
<blockquote>
<p>you spend most of the night talking with whales through music,
it is one of the <em>most magical nights of your advent of code adventures</em>.</p>
</blockquote>
<p>Gotta be honest I did not expect the final music to be so good!
I used it as soundtrack in loop while I was finishing writing the blogpost.</p>
<p>What <em>a magical night with whale music and what a great Adventure Advent of Code</em> is!</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-comment">#? replace(sub="e♭", by="dx")</span>
<span class="hljs-keyword">import</span> nimib, animu
nbInit(theme=useAdventOfNim)
nbText: <span class="hljs-string">"""## Day 3: [Binary Diagnostic](https://adventofcode.com/2021/day/3) 👯🎛️
I had to catch a plane this morning and the puzzle unlocked
while getting onto the plane. Luckily I was able to solve part1 in the few
minutes from the moment I sat on the plane to the moment they asked us
to turn off the devices, so I could get also the text for part2 and had
plenty of time during flight to solve it.
Today is also the day in which I finally realize a dream I have been having
since last edition of Advent Of Code: I want to be able to _listen to a puzzle input_
and I want to do it using the genuinely bizarre and bizarrely ingenious library [paramidi]
by one of my favorite Nim authors:
[oakes] (who is also responsible for [vim_cubed],
which I think is the most starred Nim repository after Nim compiler).
In order to do that, I accidentally released [paramidib] (not a typo) library,
which is just two templates to have better access to paramidi through nimib.
You can now follow along reading my rather dull solution for today
or head directly for [Part 3: Whale Music 🐳🎶](#whale_music).
Enjoy [Advent of Nim](https://forum.nim-lang.org/t/8657)! 🎄👑
[paramidi]: https://github.com/paranim/paramidi
[oakes]: https://github.com/oakes
[vim_cubed]: https://github.com/oakes/vim_cubed
[paramidib]: https://pietroppeter.github.io/paramidib/
### Part 1
> The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions of the submarine. The first parameter to check is the _power consumption_.
>
> You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the _gamma rate_ and the _epsilon rate_). The power consumption can then be found by multiplying the gamma rate by the epsilon rate.
>
> Each bit in the gamma rate can be determined by finding the _most common bit in the corresponding position_ of all numbers in the diagnostic report. For example, given the following diagnostic report:
"""</span>
nbCode:
<span class="hljs-keyword">let</span> testInput = <span class="hljs-string">"""
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010"""</span>
nbText: <span class="hljs-string">"""
> Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1.
>
> The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0.
>
> The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110.
>
> So, the gamma rate is the binary number 10110, or _22_ in decimal.
>
> The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001, or _9_ in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, _198_.
>
> Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. _What is the power consumption of the submarine?_
"""</span>
nbCode:
<span class="hljs-keyword">let</span>
testNums = testInput.splitLines.toSeq
puzzleNums = <span class="hljs-string">"2021/input03.txt"</span>.lines.toSeq
nbCode:
<span class="hljs-keyword">func</span> getGamma(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span>
gamma = <span class="hljs-string">""</span>
countOnes: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. nums[<span class="hljs-number">0</span>].<span class="hljs-keyword">high</span>:
countOnes = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> nums:
<span class="hljs-keyword">if</span> num[i] == <span class="hljs-string">'1'</span>:
inc countOnes
<span class="hljs-keyword">if</span> countOnes >= nums.len <span class="hljs-keyword">div</span> <span class="hljs-number">2</span>:
gamma.add <span class="hljs-string">'1'</span>
<span class="hljs-keyword">else</span>:
gamma.add <span class="hljs-string">'0'</span>
<span class="hljs-literal">result</span> = parseBinInt gamma
dump testNums.getGamma
dump puzzleNums.getGamma
gotTheStar
nbCode:
<span class="hljs-keyword">func</span> part1(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">let</span>
gamma = getGamma(nums)
<span class="hljs-comment"># 2^5(=32) - 22 - 1 -> 9 </span>
epsilon = <span class="hljs-number">2</span>^len(nums[<span class="hljs-number">0</span>]) - gamma
<span class="hljs-keyword">return</span> gamma*epsilon
dump part1 testNums
dump part1 puzzleNums
nbCode:
<span class="hljs-keyword">func</span> lifeSupport(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>], oxy=<span class="hljs-literal">true</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">var</span>
nums = nums <span class="hljs-comment"># idiomatic</span>
i = <span class="hljs-number">0</span>
zeros, ones: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]
<span class="hljs-keyword">while</span> nums.len > <span class="hljs-number">1</span>:
zeros = @[] <span class="hljs-comment"># zeros, ones = @[] does not work</span>
ones = @[]
<span class="hljs-keyword">for</span> j, num <span class="hljs-keyword">in</span> nums:
<span class="hljs-keyword">if</span> num[i] == <span class="hljs-string">'1'</span>:
ones.add j
<span class="hljs-keyword">else</span>:
zeros.add j
<span class="hljs-comment">#dump ones</span>
<span class="hljs-comment">#dump zeros</span>
<span class="hljs-keyword">if</span> oxy:
<span class="hljs-keyword">if</span> ones.len >= zeros.len:
nums = collect:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> ones:
nums[j]
<span class="hljs-keyword">else</span>:
nums = collect:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> zeros:
nums[j]
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">if</span> zeros.len <= ones.len:
nums = collect:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> zeros:
nums[j]
<span class="hljs-keyword">else</span>:
nums = collect:
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> ones:
nums[j]
inc i
<span class="hljs-comment">#debugEcho nums</span>
<span class="hljs-keyword">assert</span> nums.len == <span class="hljs-number">1</span>
<span class="hljs-keyword">return</span> parseBinInt(nums[<span class="hljs-number">0</span>])
<span class="hljs-keyword">func</span> part2(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
lifeSupport(nums)*lifeSupport(nums, <span class="hljs-literal">false</span>)
dump part2 testNums
dump part2 puzzleNums
gotTheStar
<span class="hljs-comment"># add an anchor</span>
nbText: <span class="hljs-string">"""### Part 3: <a name="whale_music">Whale Music 🐳🎶</a>
> on top of the odd creaking noises that the submarine has been making in its descent,
> you start to hear a more pleasant melody that vibrates all through the hull.
> By looking outside the small circular windows you realize that you are surrounded
> by [singing whales](https://www.nationalgeographic.com/magazine/graphics/here-is-how-whales-sing-from-yaps-to-fin-slaps-feature)!
>
> Having already completed your star collection for the day,
> you decide to dedicate the rest of the day (and night) to learn more about this songs.
>
> Luckily, the _library of the submarnim_ has a small booklet by one mysterious
> [Kaz Cohaes](https://sekao.net) entitled
> [paralipomena of phalenotragoùdia](https://it.wikipedia.org/wiki/Paralipomeni_della_Batracomiomachia)
> that promises through the [lost art of midi](https://github.com/oakes/ansiwave) to
> allow you to be able to communicate with the singing whales!
>
> You are definitely in luck today, since it appears that the key ingredient to make
> such a communication possible is a _diagnostic report_ like the one produced by your submarine!
> You also happen to have the
> latest version of terminal _N.I.M.I.B._ (Natural Inducer of Music for Interesting Balenas)
> which you will use to solve the exercises proposed by the booklet.
>
> The first chapter of PoP (short for Paralipomena of phalenotragoùdia) starts with the basic
> assumption of whale singing. Communication happen through _musical scales_ and
> the utmost expressiveness is reached through _alternating moments of music and moments of silence_.
> Your first exercise is to translate your test diagnostic report into music.
>
> _Each line of the report is the repetition of the same scale, with the zeros representing
> moments of silence, while when a _1_ is present the note must be emitted_.
We will use library [paramidi] to produce the music, but we import it as [paramidib] (not a typo).
This is a convenience thin layer that exposes two templates to save a paramidi score
to a wave file and then to expose such a wave file through an html audio control.
Since the test diagnostic report is made of strings of 5 digits we will use a
[pentatonic scale](https://en.wikipedia.org/wiki/Pentatonic_scale).
The primary way to write a paramidi music score is through a bizarre data structure
which is a rather freely constructed hierarchy of tuples.
If you find an instrument in the tuple, it means that what follows will be played
with that instrument, a tempo annotation sets the tempo, a ratio of integers sets the
length of next note, then finally come the notes which have the usual syntax through
letter of the alphabet (there are ways also to change octave and to write chromatic notes).
A letter _r_ denotes a moment of rest (silence).
For the first exercise we will encode manually the score.
[paramidi]: https://github.com/paranim/paramidi
[paramidib]: https://github.com/pietroppeter/paramidib
"""</span>
nbCode:
<span class="hljs-keyword">import</span> paramidib
saveMusic(<span class="hljs-string">"2021/day03test.wav"</span>):
(piano,
(tempo: <span class="hljs-number">109</span>), <span class="hljs-comment"># allegretto</span>
<span class="hljs-number">1</span>/<span class="hljs-number">5</span>,
<span class="hljs-comment"># A C D E G </span>
<span class="hljs-comment"># 0 0 1 0 0</span>
r,r,d,r,r,
<span class="hljs-comment"># 1 1 1 1 0</span>
a,c,d,e,r,
<span class="hljs-comment"># 1 0 1 1 0</span>
a,r,d,e,r,
<span class="hljs-comment"># 1 0 1 1 1</span>
a,r,d,e,g,
<span class="hljs-comment"># 1 0 1 0 1</span>
a,r,d,r,g,
<span class="hljs-comment"># 0 1 1 1 1</span>
r,c,d,e,g,
<span class="hljs-comment"># 0 0 1 1 1</span>
r,r,d,e,g,
<span class="hljs-comment"># 1 1 1 0 0</span>
a,c,d,r,r,
<span class="hljs-comment"># 1 0 0 0 0</span>
a,r,r,r,r,
<span class="hljs-comment"># 1 1 0 0 1</span>
a,c,r,r,g,
<span class="hljs-comment"># 0 0 0 1 0</span>
r,r,r,e,r,
<span class="hljs-comment"># 0 1 0 1 0</span>
r,c,r,e,r
)
nbAudio(<span class="hljs-string">"2021/day03test.wav"</span>)
nbText: <span class="hljs-string">"""
Good job on producing the first piece of music!
> The second chapter of PoP tells you that longer sequence of digits
> may represent [arpeggios](https://en.wiktionary.org/wiki/arpeggio) of scales
> going up and down
>
> The third chapter tells you that whale are apparently fond a [bop](https://en.wikipedia.org/wiki/Bebop)
> and they like the color [blue](https://en.wikipedia.org/wiki/Blue).
Since the puzzle input is made of sequences of 12 digits, we will use
a [blues scale](https://en.wikipedia.org/wiki/Blues_scale) which is made of 6 notes
and we will go up and down the scale with an arpeggio.
Here is how this scale sound:
"""</span>
nbCode:
<span class="hljs-comment"># blues scale: A C D Eb E G</span>
saveMusic(<span class="hljs-string">"2021/blues_scale.wav"</span>):
(altosax,
(tempo: <span class="hljs-number">234</span>), <span class="hljs-number">2</span>/<span class="hljs-number">12</span>,
-a, c, d, e♭, e, g,
a, g, e, e♭, d, c,
-a, c, d, e♭, e, g,
a, g, e, e♭, d, c,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, -a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, -a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c,
)
nbAudio(<span class="hljs-string">"2021/blues_scale.wav"</span>)
nbText: <span class="hljs-string">"""
Note that:
- we pass from a straight ryhthm to [triplet swing](https://en.wikipedia.org/wiki/Swing_(jazz_performance_style))
- the instrument is a sax alto and the tempo is the same of Charlie Bird's [Ornithology](https://getsongbpm.com/song/ornithology/MjKLP5) (_prestissimo_)
- `-a` is used for a `a` one octave lower
- `e♭` is actually substituted through a [source code filter](https://nim-lang.org/docs/filters.html) to `dx` which represents `d#`
Now let's try to see how this would look with the moments of silence on zeros.
We take the forst two lines of puzzle input to test how it sounds:
"""</span>
nbCode:
<span class="hljs-keyword">echo</span> puzzleNums[<span class="hljs-number">0</span> .. <span class="hljs-number">1</span>]
nbCode:
saveMusic(<span class="hljs-string">"2021/day03puzzle_head.wav"</span>):
(altosax,
(tempo: <span class="hljs-number">234</span>),
<span class="hljs-comment"># 1 1 1 1 1 1</span>
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, -a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g,
<span class="hljs-comment"># 0 1 0 0 1 1</span>
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, r , <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, d, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c,
<span class="hljs-comment"># 1 1 0 0 1 1</span>
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, -a, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, c, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, r , <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, g,
<span class="hljs-comment"># 0 0 1 1 0 0</span>
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, e, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, e♭, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, r, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, r,
)
nbAudio(<span class="hljs-string">"2021/day03puzzle_head.wav"</span>)
nbText: <span class="hljs-string">"""
A tuple is an immutable data structure and since Nim is strongly typed it
is not the most flexible tool to write a score at runtime.
Luckily paramidi offers another way to write scores using `json` variant objects.
"""</span>
nbCode:
<span class="hljs-keyword">import</span> json
<span class="hljs-keyword">let</span> puzzleHead =
%*[<span class="hljs-string">"alto-sax"</span>, {<span class="hljs-string">"tempo"</span>: <span class="hljs-number">234</span>},
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"a-"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"c"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"d"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"d#"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"e"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"g"</span>,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"g"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span> , <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"d"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"c"</span>,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"a-"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"c"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span> , <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"e"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"g"</span>,
<span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"e"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"d#"</span>, <span class="hljs-number">2</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>, <span class="hljs-number">1</span>/<span class="hljs-number">12</span>, <span class="hljs-string">"r"</span>,
]
saveMusic(<span class="hljs-string">"2021/day03puzzle_head_alt.wav"</span>, puzzle_head)
nbAudio(<span class="hljs-string">"2021/day03puzzle_head_alt.wav"</span>)
nbText: <span class="hljs-string">"""
Note that:
- instead of `altosax` we need to use `alto-sax`
- instead of `-a` we use `a-`
- instead of `dx` we use `d#`
In fact all this alternative syntax is the most natural one since it derives
from the C library that paramidi used to produce the sound.
See the list of available constants [here](https://github.com/paranim/paramidi/blob/master/src/paramidi/constants.nim).
Now we are ready to create the score starting from a list of inputs in an automatic way:
"""</span>
nbCode:
<span class="hljs-keyword">func</span> getScore(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>], denominator=<span class="hljs-number">12</span>): <span class="hljs-type">JsonNode</span> =
<span class="hljs-keyword">const</span> arpeggio = @[<span class="hljs-string">"a-"</span>, <span class="hljs-string">"c"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"d#"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"g"</span>, <span class="hljs-string">"a"</span>, <span class="hljs-string">"g"</span>, <span class="hljs-string">"e"</span>, <span class="hljs-string">"d#"</span>, <span class="hljs-string">"d"</span>, <span class="hljs-string">"c"</span>]
<span class="hljs-literal">result</span> = %*[<span class="hljs-string">"alto-sax"</span>, {<span class="hljs-string">"tempo"</span>: <span class="hljs-number">234</span>}]
<span class="hljs-keyword">var</span>
beat = <span class="hljs-literal">true</span>
i = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> nums:
<span class="hljs-keyword">for</span> digit <span class="hljs-keyword">in</span> num:
<span class="hljs-keyword">if</span> beat:
<span class="hljs-literal">result</span>.add %(<span class="hljs-number">2</span>/denominator)
<span class="hljs-keyword">else</span>:
<span class="hljs-literal">result</span>.add %(<span class="hljs-number">1</span>/denominator)
<span class="hljs-keyword">if</span> digit == <span class="hljs-string">'1'</span>:
<span class="hljs-literal">result</span>.add %(arpeggio[i <span class="hljs-keyword">mod</span> <span class="hljs-number">12</span>])
<span class="hljs-keyword">else</span>:
<span class="hljs-literal">result</span>.add %<span class="hljs-string">"r"</span>
inc i
beat = <span class="hljs-keyword">not</span> beat
saveMusic(<span class="hljs-string">"2021/day03puzzle_head_longer.wav"</span>, getScore(puzzleNums[<span class="hljs-number">0</span> ..< <span class="hljs-number">10</span>]))
saveMusic(<span class="hljs-string">"2021/day03puzzle_head_faster.wav"</span>, getScore(puzzleNums[<span class="hljs-number">0</span> ..< <span class="hljs-number">20</span>], denominator=<span class="hljs-number">24</span>))
nbAudio(<span class="hljs-string">"2021/day03puzzle_head_longer.wav"</span>)
nbAudio(<span class="hljs-string">"2021/day03puzzle_head_faster.wav"</span>)
nbText: <span class="hljs-string">"""We can also decide that we want to hear stuff at double the velocity.
Finally, I cannot put the full song since it is too big for github, but here
is a 10 minute excerpt (full song is around 25 minutes):"""</span>
nbCode:
saveMusic(<span class="hljs-string">"2021/day03puzzle.wav"</span>, getScore(puzzleNums[<span class="hljs-number">0</span> .. <span class="hljs-number">400</span>]))
saveMusic(<span class="hljs-string">"2021/day03puzzle_faster.wav"</span>, getScore(puzzleNums[<span class="hljs-number">0</span> .. <span class="hljs-number">400</span>], denominator=<span class="hljs-number">24</span>))
nbAudio(<span class="hljs-string">"2021/day03puzzle.wav"</span>)
nbAudio(<span class="hljs-string">"2021/day03puzzle_faster.wav"</span>)
nbText: <span class="hljs-string">"""
> you spend most of the night talking with whales through music,
> it is one of the _most magical nights of your advent of code adventures_.
Gotta be honest I did not expect the final music to be so good!
I used it as soundtrack in loop while I was finishing writing the blogpost.
What _a magical night with whale music and what a great Adventure Advent of Code_ is!
"""</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>