-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent-of-code-2021.html
1007 lines (901 loc) · 92.4 KB
/
advent-of-code-2021.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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2022-W11-4 02:12 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Advent Of Code 2021</title>
<meta name="author" content="Inanna" />
<meta name="description" content="A series of fun problems that I have solved for the 2021 advent of code." />
<meta name="generator" content="Org Mode" />
<link href="/site.css" rel="stylesheet" type="text/css" /><link href="images/website-icon.png" rel="icon" />
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "center",
displayIndent: "0em",
"HTML-CSS": { scale: 100,
linebreaks: { automatic: "false" },
webFont: "TeX"
},
SVG: {scale: 100,
linebreaks: { automatic: "false" },
font: "TeX"},
NativeMML: {scale: 100},
TeX: { equationNumbers: {autoNumber: "AMS"},
MultLineWidth: "85%",
TagSide: "right",
TagIndent: ".8em"
}
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="preamble" class="status">
<div><a href="/index.html"><img alt="An abstract logo representing a series of three assembly line stamping machines with the words CONS, DEV, and embalzoned in white on each machine." id="site-logo" src="/images/website-logo.png" /></a></div>
</div>
<div id="content" class="content">
<h1 class="title">Advent Of Code 2021</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#orgdb9d176">Introduction</a></li>
<li><a href="#orgf94b6a6">Utility Functions</a></li>
<li><a href="#org5d9c6fa">Day 1: Sonar Sweep</a>
<ul>
<li><a href="#org9535a6e">Part A</a></li>
<li><a href="#orge1c1445">Part B</a></li>
</ul>
</li>
<li><a href="#orgfe3c0e0">Day 2: Dive!</a>
<ul>
<li><a href="#orgbed6f5c">Part A</a></li>
<li><a href="#org7048783">Part B</a></li>
</ul>
</li>
<li><a href="#org202ca64">Day 3: Binary Diagnostic</a>
<ul>
<li><a href="#org0e0b333">Part A</a></li>
<li><a href="#org28db914">Part B</a></li>
</ul>
</li>
<li><a href="#org033bdfa">Day 4: Giant Squid</a>
<ul>
<li><a href="#org604d9ca">Common</a>
<ul>
<li><a href="#org3636be8">won?</a></li>
<li><a href="#org208c088">vectorify</a></li>
<li><a href="#org2946f90">score-board</a></li>
</ul>
</li>
<li><a href="#org6b28160">Part A</a></li>
<li><a href="#org72f11a0">Part B</a></li>
<li><a href="#orgdeabf3d">A Better Version</a>
<ul>
<li><a href="#org232c322">Game</a></li>
<li><a href="#org6010edb">play-round</a></li>
<li><a href="#org641de19">make-game</a></li>
<li><a href="#orgbc9186b">score-winners</a></li>
<li><a href="#org0a52225">Putting It Together</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgd0cc6f8">Day 5: Hydrothermal Venture</a>
<ul>
<li><a href="#org7c98c5c">Common</a>
<ul>
<li><a href="#orgff1d94f">draw-line-segment</a></li>
<li><a href="#org3db266e">find-intersections</a></li>
<li><a href="#org1d9a6b0">parse-lines</a></li>
</ul>
</li>
<li><a href="#orgdc52816">Part A</a></li>
<li><a href="#org84aad6e">Part B</a></li>
</ul>
</li>
<li><a href="#orga0adb3a">Day 6: Lanternfish</a>
<ul>
<li><a href="#orga1c3434">Part A</a></li>
<li><a href="#orgea6b53b">Part B</a></li>
</ul>
</li>
<li><a href="#org1f5ef0b">Day 7: The Treachery of Whales</a>
<ul>
<li><a href="#org355ad4d">Part A</a></li>
<li><a href="#org768213e">Part B</a></li>
</ul>
</li>
<li><a href="#org1da0441"><span class="todo TODO">TODO</span> Day 8: Seven Segment Search</a>
<ul>
<li><a href="#orga6d25a7">Part A</a></li>
<li><a href="#org82a88a0">Part B</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-orgdb9d176" class="outline-2">
<h2 id="orgdb9d176">Introduction</h2>
<div class="outline-text-2" id="text-orgdb9d176">
<p>
The advent of code competition is a fun yearly event that I like to participate in occasionally.
</p>
</div>
</div>
<div id="outline-container-orgf94b6a6" class="outline-2">
<h2 id="orgf94b6a6">Utility Functions</h2>
<div class="outline-text-2" id="text-orgf94b6a6">
<p>
To start out I will define a utility function to make reading EDN files easier. Because Clojure uses the JVM it usually just assumes you want paths from the project root, whereas in this case I want paths coming from this directory.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span>require '<span style="color: #bbb;">[</span>clojure.java.io <span style="color: #E53935;">:as</span> io<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">read-edn</span> <span style="color: #bbb;">[</span>f<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> f <span style="color: #E53935;">io</span>/resource slurp read-string<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">read-file</span> <span style="color: #bbb;">[</span>f<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> f <span style="color: #E53935;">io</span>/resource slurp<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org5d9c6fa" class="outline-2">
<h2 id="org5d9c6fa">Day 1: Sonar Sweep</h2>
<div class="outline-text-2" id="text-org5d9c6fa">
<p>
The first problem is fairly simple. We are to take a list of numbers and compare each pair within the list, producing.
</p>
</div>
<div id="outline-container-org9535a6e" class="outline-3">
<h3 id="org9535a6e">Part A</h3>
<div class="outline-text-3" id="text-org9535a6e">
<p>
Once that is done we are to add up the total number of increases. This is fairly simple, and the following code does that through a simple recursive function and list destructing.<label class="sidenote-number" for="1"><sup>1</sup></label><input checked="checked" id="1" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 1</span> List destructuring is really one of my favorite bits of Clojure's syntax. Yes, it's common, but it's also really useful.</span> We take the first two elements of the list, and compare them to check if they have increased, returning 1 if they have, 0 if they have not. Then we check if the list is empty, if it is we simply return, but if it is not we sum with the function applied to the rest of the list minus the first element.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.1.a</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-edn<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">count-increases</span>
<span style="color: #bbb;">[</span><span style="color: #494949;">[</span>a b & rst<span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>rval <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>< a b<span style="color: #494949;">)</span> 1 0<span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span>empty? rst<span style="color: #bbb;">)</span>
rval
<span style="color: #bbb;">(</span>+ rval <span style="color: #494949;">(</span>count-increases <span style="color: #bbb;">(</span>cons b rst<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>count-increases <span style="color: #bbb;">(</span>read-edn <span style="color: #494949;">"./other/advent-of-code-2021-01_input.edn"</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orge1c1445" class="outline-3">
<h3 id="orge1c1445">Part B</h3>
<div class="outline-text-3" id="text-orge1c1445">
<p>
The second problem is the generation of a three measurement sliding window. To solve this we make a slight modification, adding some further elements in the destructuring bind. Once that is done it's smooth sailing.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.1.b</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-edn<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">count-average-increases</span>
<span style="color: #bbb;">[</span><span style="color: #494949;">[</span>a b c d & rst<span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>rval <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>< <span style="color: #bbb;">(</span>+ a b c<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>+ b c d<span style="color: #bbb;">)</span><span style="color: #494949;">)</span> 1 0<span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span>empty? rst<span style="color: #bbb;">)</span>
rval
<span style="color: #bbb;">(</span>+ rval <span style="color: #494949;">(</span>count-average-increases <span style="color: #bbb;">(</span>concat <span style="color: #494949;">[</span>b c d<span style="color: #494949;">]</span> rst<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">ls</span> <span style="color: #bbb;">[</span>s<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span>+ s 3<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>count-average-increases <span style="color: #bbb;">(</span>read-edn <span style="color: #494949;">"./other/advent-of-code-2021-01_input.edn"</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>ls 5<span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgfe3c0e0" class="outline-2">
<h2 id="orgfe3c0e0">Day 2: Dive!</h2>
<div class="outline-text-2" id="text-orgfe3c0e0">
<p>
In both cases it was possible to solve the problems in \(O(n)\) time. However, unfortunately, unlike the last one this probably could not be parallelized for matrix math, like I have seen with some of my friends.
</p>
</div>
<div id="outline-container-orgbed6f5c" class="outline-3">
<h3 id="orgbed6f5c">Part A</h3>
<div class="outline-text-3" id="text-orgbed6f5c">
<p>
Here we write a simple bit of code to calculate the position of the craft. Here we simply have the string representing the file pre-converted into an EDN.<label class="sidenote-number" for="2"><sup>2</sup></label><input checked="checked" id="2" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 2</span> I think this is reasonably within the rules. The lists are basically structured so that I only have to add an extra pair of square brackets to make it a vector.</span> Then we read it in, partition it into a series of 2-tuples, use some destructuring to conduct math to add it to a vector (\(i\) represents the horizontal, \(j\) the vertical) and then we multiply our values together.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.2.a</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-edn<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">"./other/advent-of-code-2021-02_input.edn"</span>
read-edn
<span style="color: #bbb;">(</span>partition 2<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>reduce <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span><span style="color: #494949;">[</span>i j<span style="color: #494949;">]</span> <span style="color: #494949;">[</span>dir val<span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">case</span> dir
forward <span style="color: #494949;">[</span><span style="color: #bbb;">(</span>+ i val<span style="color: #bbb;">)</span> j<span style="color: #494949;">]</span>
up <span style="color: #494949;">[</span>i <span style="color: #bbb;">(</span>- j val<span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
down <span style="color: #494949;">[</span>i <span style="color: #bbb;">(</span>+ j val<span style="color: #bbb;">)</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">[</span>0 0<span style="color: #494949;">]</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>apply *<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org7048783" class="outline-3">
<h3 id="org7048783">Part B</h3>
<div class="outline-text-3" id="text-org7048783">
<p>
To solve part two we now need to treat the <code>up</code> and <code>down</code> pairs as representing changes to the slope of the list. To do so we simply add a new value representing the slope to the accumulator, known as <code>aim</code>. We then, when looping over it, modify the aim value for up and down commands, and modify the depth during forward commands.
</p>
<div class="org-src-container">
<pre class="src src-clojure"> <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.2.b</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-edn<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">"./other/advent-of-code-2021-02_input.edn"</span>
read-edn
<span style="color: #bbb;">(</span>partition 2<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>reduce <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span><span style="color: #494949;">[</span>i j aim<span style="color: #494949;">]</span> <span style="color: #494949;">[</span>dir val<span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">case</span> dir
forward <span style="color: #494949;">[</span><span style="color: #bbb;">(</span>+ i val<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>+ j <span style="color: #494949;">(</span>* val aim<span style="color: #494949;">)</span><span style="color: #bbb;">)</span> aim<span style="color: #494949;">]</span>
up <span style="color: #494949;">[</span>i j <span style="color: #bbb;">(</span>- aim val<span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
down <span style="color: #494949;">[</span>i j <span style="color: #bbb;">(</span>+ aim val<span style="color: #bbb;">)</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">[</span>0 0 0<span style="color: #494949;">]</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>take 2<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>apply *<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org202ca64" class="outline-2">
<h2 id="org202ca64">Day 3: Binary Diagnostic</h2>
<div class="outline-text-2" id="text-org202ca64">
<p>
Whew, these solutions took longer. I think part of what shot me in the foot here was that my solutions were too cute in many ways, unnecessarily using map / reduce functions to process the data, making it harder to read and work with than needed, and as it has been said,
</p>
<blockquote>
<p>
Programs must be written for people to read, and only incidentally for machines to execute.
</p>
<p>
– Harld Abelson, The Structure and Interpretation of Computer Programs
</p>
</blockquote>
</div>
<div id="outline-container-org0e0b333" class="outline-3">
<h3 id="org0e0b333">Part A</h3>
<div class="outline-text-3" id="text-org0e0b333">
<p>
Here in our first binary diagnostic we write a rather simple bit of code, really not much more than we would normally do, though the program logic is a bit twistier than the usual.
</p>
<p>
Basically we take the list, reducing over each element with a function that maps over each character. We add the numeric value of the character, taken by subtracting 48 from it, the ASCII value of the character 0, to the accumulator at the index. Following this we map over our list, selecting the largest values, smoosh it together into a string, and convert it to an integer by parsing the list from binary, which gives us gamma.
</p>
<p>
Next we simply do a bitwise xor with 12 bits of 1, giving us epsilon.
</p>
<p>
After this we can just multiply them togetehr.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">advent-of-code.day3.1</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>clojure.java.io <span style="color: #E53935;">:as</span> io<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #bbb;">[</span>bin <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">"./other/advent-of-code-2021-03_input.txt"</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">io</span>/resource<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>slurp<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">str</span>/split-lines<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
ɣ <span style="color: #494949;">(</span><span style="color: #E53935;">Integer</span>/parseInt
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">(</span>reduce <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span>acc e<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>map #<span style="color: #bbb;">(</span>+ <span style="color: #494949;">(</span>- <span style="color: #bbb;">(</span>int <span style="font-style: italic;">%1</span><span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>int <span style="color: #494949;">\0</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span> <span style="font-style: italic;">%2</span><span style="color: #bbb;">)</span>
e acc<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>repeat 12 0<span style="color: #bbb;">)</span> bin<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map #<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>< <span style="color: #bbb;">(</span>/ <span style="color: #494949;">(</span>count bin<span style="color: #494949;">)</span> 2<span style="color: #bbb;">)</span> <span style="font-style: italic;">%</span><span style="color: #494949;">)</span> 1 0<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>apply str<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
2<span style="color: #494949;">)</span>
ɛ <span style="color: #494949;">(</span>bit-xor ɣ 0xfff<span style="color: #494949;">)</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>* ɣ ɛ<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org28db914" class="outline-3">
<h3 id="org28db914">Part B</h3>
<div class="outline-text-3" id="text-org28db914">
<p>
Here is the fun part, and the part where we apply recursion<label class="sidenote-number" for="3"><sup>3</sup></label><input checked="checked" id="3" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 3</span> See recursion.</span> to solve the problem, and also put ourselves in a somewhat strange world of program logic.
</p>
<p>
Here we basically define a function that filters the list recursively until only element remains. As the list is approximately halved with each search the total runtime is \(O(n \times log(n))\).
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">advent-of-code.day3.1</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>clojure.java.io <span style="color: #E53935;">:as</span> io<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">bit-filter</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>lst comp<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>bit-filter lst comp 0<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>lst comp pos<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span>> <span style="color: #494949;">(</span>count lst<span style="color: #494949;">)</span> 1<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>fval <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>comp
<span style="color: #bbb;">(</span>reduce <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span>acc e<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>+ <span style="color: #494949;">(</span>- <span style="color: #bbb;">(</span>int <span style="color: #494949;">(</span>nth e pos<span style="color: #494949;">)</span><span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>int <span style="color: #494949;">\0</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span> acc<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
0 lst<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>/ <span style="color: #494949;">(</span>count lst<span style="color: #494949;">)</span> 2<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">\1</span>
<span style="color: #494949;">\0</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>bit-filter <span style="color: #bbb;">(</span>filter #<span style="color: #494949;">(</span>= <span style="color: #bbb;">(</span>nth <span style="font-style: italic;">%</span> pos<span style="color: #bbb;">)</span> fval<span style="color: #494949;">)</span> lst<span style="color: #bbb;">)</span> comp <span style="color: #bbb;">(</span>inc pos<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">Integer</span>/parseInt <span style="color: #494949;">(</span>first lst<span style="color: #494949;">)</span> 2<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #bbb;">[</span>bin <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">"./other/advent-of-code-2021-03_input.txt"</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">io</span>/resource<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>slurp<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">str</span>/split-lines<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
oxygen-rating <span style="color: #494949;">(</span>bit-filter bin >=<span style="color: #494949;">)</span>
co2-rating <span style="color: #494949;">(</span>bit-filter bin <<span style="color: #494949;">)</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>* co2-rating oxygen-rating<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org033bdfa" class="outline-2">
<h2 id="org033bdfa">Day 4: Giant Squid</h2>
<div class="outline-text-2" id="text-org033bdfa">
<p>
Here we simply write a bit of code to find the winning bingo board and cheat. Ah, cheating, the best way to use code.
</p>
</div>
<div id="outline-container-org604d9ca" class="outline-3">
<h3 id="org604d9ca">Common</h3>
<div class="outline-text-3" id="text-org604d9ca">
</div>
<div id="outline-container-org3636be8" class="outline-4">
<h4 id="org3636be8">won?</h4>
<div class="outline-text-4" id="text-org3636be8">
<p>
The won predicate <label class="sidenote-number" for="4"><sup>4</sup></label><input checked="checked" id="4" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 4</span> Also known as a test function.</span> takes two arguments, a list of numbers already played as well as a single board. It then extracts the possible score positions from the board and combines them into a list. Following this it proceeds to check if any of them are a subset of the set of nums. Should this be the case it returns true.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org55d7c1c"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">won?</span> <span style="color: #bbb;">[</span>nums board<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">(</span>concat board
<span style="color: #bbb;">(</span>map <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span>col-nth<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span>map #<span style="color: #494949;">(</span>nth <span style="font-style: italic;">%</span> col-nth<span style="color: #494949;">)</span> board<span style="color: #bbb;">)</span><span style="color: #494949;">)</span> <span style="color: #494949;">(</span>range 5<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>some #<span style="color: #bbb;">(</span>subset? <span style="font-style: italic;">%</span> nums<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org208c088" class="outline-4">
<h4 id="org208c088">vectorify</h4>
<div class="outline-text-4" id="text-org208c088">
<p>
This is a simple recursive function that just converts everything into a series of vectors, mostly for when I need vectors but I don't get them initially.<label class="sidenote-number" for="5"><sup>5</sup></label><input checked="checked" id="5" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 5</span> This is only used once, but having a function makes what I am doing clearer.</span>
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org8c89b0a"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">vectorify</span> <span style="color: #bbb;">[</span>c<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>coll? c<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>vec <span style="color: #bbb;">(</span>map vectorify c<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
c<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org2946f90" class="outline-4">
<h4 id="org2946f90">score-board</h4>
<div class="outline-text-4" id="text-org2946f90">
<p>
Here our score function that takes the list of numbers played as well as a board and converts it into a score.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org74272ce"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">score-board</span> <span style="color: #bbb;">[</span>nums board<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>* <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> board
flatten
<span style="color: #bbb;">(</span>filter #<span style="color: #494949;">(</span>not <span style="color: #bbb;">(</span>contains? <span style="color: #494949;">(</span>set nums<span style="color: #494949;">)</span> <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>apply +<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>last nums<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<pre class="example">
#'aoc.4.a/score-board
</pre>
</div>
</div>
</div>
<div id="outline-container-org6b28160" class="outline-3">
<h3 id="org6b28160">Part A</h3>
<div class="outline-text-3" id="text-org6b28160">
<p>
Now we put it all together using the <a href="https://orgmode.org/manual/Noweb-Reference-Syntax.html">noweb syntax</a> in org-mode.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.4.a</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.set <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>subset?<span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-edn<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<<vectorify>>
<<won>>
<<score-board>>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span>raw-boards <span style="color: #E53935;">:boards</span>
moves <span style="color: #E53935;">:moves</span><span style="color: #494949;">}</span> <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">"./other/advent-of-code-2021-04_input.edn"</span>
read-edn<span style="color: #494949;">)</span>
boards <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> raw-boards
<span style="color: #bbb;">(</span>partition 25<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>map <span style="color: #494949;">(</span>partial partition 5<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
vectorify<span style="color: #494949;">)</span>
<span style="color: #494949;">[</span>mov-num winner<span style="color: #494949;">]</span> <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #bbb;">(</span>range <span style="color: #494949;">(</span>count moves<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>some <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span>mov-num<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>winner
<span style="color: #bbb;">(</span>filter <span style="color: #494949;">(</span>partial won? <span style="color: #bbb;">(</span>set <span style="color: #494949;">(</span>take mov-num moves<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
boards<span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">when</span> <span style="color: #bbb;">(</span>not <span style="color: #494949;">(</span>empty? winner<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">[</span>mov-num winner<span style="color: #bbb;">]</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>score-board <span style="color: #494949;">(</span>take mov-num moves<span style="color: #494949;">)</span> winner<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org72f11a0" class="outline-3">
<h3 id="org72f11a0">Part B</h3>
<div class="outline-text-3" id="text-org72f11a0">
<p>
How for part B we start working on finding the last board to score. To do this we make a slight modification to our initial function and simply ask for the last board to win by filtering out the winning boards until none are left.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.4.b</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.set <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>subset? difference<span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-edn<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<<vectorify>>
<<won>>
<<score-board>>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span>raw-boards <span style="color: #E53935;">:boards</span>
moves <span style="color: #E53935;">:moves</span><span style="color: #494949;">}</span> <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">"./other/advent-of-code-2021-04_input.edn"</span>
read-edn<span style="color: #494949;">)</span>
boards <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> raw-boards
<span style="color: #bbb;">(</span>partition 25<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>map <span style="color: #494949;">(</span>partial partition 5<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
vectorify<span style="color: #494949;">)</span>
<span style="color: #494949;">[</span>mov-num looser<span style="color: #494949;">]</span> <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #bbb;">(</span>range <span style="color: #494949;">(</span>count moves<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>some <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span>mov-num<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>loosers
<span style="color: #bbb;">(</span>filter #<span style="color: #494949;">(</span>not <span style="color: #bbb;">(</span>won? <span style="color: #494949;">(</span>set <span style="color: #bbb;">(</span>take mov-num moves<span style="color: #bbb;">)</span><span style="color: #494949;">)</span> <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
boards<span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">when</span> <span style="color: #bbb;">(</span>empty? loosers<span style="color: #bbb;">)</span>
<span style="color: #bbb;">[</span>mov-num <span style="color: #494949;">(</span>filter #<span style="color: #bbb;">(</span>not <span style="color: #494949;">(</span>won? <span style="color: #bbb;">(</span>set <span style="color: #494949;">(</span>take <span style="color: #bbb;">(</span>dec mov-num<span style="color: #bbb;">)</span> moves<span style="color: #494949;">)</span><span style="color: #bbb;">)</span> <span style="font-style: italic;">%</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
boards<span style="color: #494949;">)</span><span style="color: #bbb;">]</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>score-board <span style="color: #494949;">(</span>take mov-num moves<span style="color: #494949;">)</span> looser<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgdeabf3d" class="outline-3">
<h3 id="orgdeabf3d">A Better Version</h3>
<div class="outline-text-3" id="text-orgdeabf3d">
<p>
Now I wasn't fully satisfied with that approach as I don't think it is very idiomatic or clear. After finishing <a href="#orga0adb3a">Day 6: Lanternfish</a> I realized that a better approach would be to create a function that plays individual rounds of the game, producing lists containing the winners for that round when repeatedly applied.
</p>
<p>
To do this I create a game object which contains information about the current round, the boards that have won this round, and the boards that have not won. Each iteration of this therefore produces a map of the game state
</p>
</div>
<div id="outline-container-org232c322" class="outline-4">
<h4 id="org232c322">Game</h4>
<div class="outline-text-4" id="text-org232c322">
<p>
This is the game record. I chose a record rather than a map because it would be more explicit about the structure of the actual data and more effectively constrains it.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="orgd64b1a2"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defrecord</span> <span style="color: #E53935;">Game</span> <span style="color: #bbb;">[</span>round nums boards winners<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org6010edb" class="outline-4">
<h4 id="org6010edb">play-round</h4>
<div class="outline-text-4" id="text-org6010edb">
<p>
Now we can write some code to play a single round. As you can see we simply take the winners and set the new game <code>:boards</code> field to contain only the set of boards that have not won yet while the <code>:winners</code> field contains all boards that have won thus far. This represents a single round played by the game.
</p>
<div class="org-src-container">
<pre class="src src-clojure"> <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">play-round</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span>round <span style="color: #E53935;">:round</span> nums <span style="color: #E53935;">:nums</span> boards <span style="color: #E53935;">:boards</span> winners <span style="color: #E53935;">:winners</span><span style="color: #494949;">}</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>new-round <span style="color: #bbb;">(</span>inc round<span style="color: #bbb;">)</span>
winners <span style="color: #bbb;">(</span>set <span style="color: #494949;">(</span>filter <span style="color: #bbb;">(</span>partial won? <span style="color: #494949;">(</span>set <span style="color: #bbb;">(</span>take new-round nums<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span> boards<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>->Game new-round
nums
<span style="color: #bbb;">(</span>difference boards winners<span style="color: #bbb;">)</span>
winners<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org641de19" class="outline-4">
<h4 id="org641de19">make-game</h4>
<div class="outline-text-4" id="text-org641de19">
<p>
The make-game function is also fairly simple. We move the partitioning and loading logic into it, simply taking a file of the correct format and converting it into a game at round 0.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org874aac6"> <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">make-game</span> <span style="color: #bbb;">[</span>f<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span><span style="color: #bbb;">{</span>raw-boards <span style="color: #E53935;">:boards</span>
moves <span style="color: #E53935;">:moves</span><span style="color: #bbb;">}</span> <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> f
read-edn<span style="color: #bbb;">)</span>
boards <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> raw-boards
<span style="color: #494949;">(</span>partition 25<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map <span style="color: #bbb;">(</span>partial partition 5<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
vectorify<span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>->Game 0 moves <span style="color: #bbb;">(</span>set boards<span style="color: #bbb;">)</span> #<span style="color: #bbb;">{}</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgbc9186b" class="outline-4">
<h4 id="orgbc9186b">score-winners</h4>
<div class="outline-text-4" id="text-orgbc9186b">
<p>
Now we have a little function for scoring each of the winners in a game which uses the <code>score-board</code> function for the scoring logic.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org2b62df8"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">score-winners</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span>nums <span style="color: #E53935;">:nums</span> round <span style="color: #E53935;">:round</span> winners <span style="color: #E53935;">:winners</span><span style="color: #494949;">}</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>map <span style="color: #494949;">(</span>partial score-board <span style="color: #bbb;">(</span>take <span style="color: #494949;">(</span>dec round<span style="color: #494949;">)</span> nums<span style="color: #bbb;">)</span><span style="color: #494949;">)</span> winners<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org0a52225" class="outline-4">
<h4 id="org0a52225">Putting It Together</h4>
<div class="outline-text-4" id="text-org0a52225">
<p>
As we can see we now have some functions to simply simulate a game played over time as a series of different lists. From here we simply have to use a function to take numbers from it until it reaches the correct answer, and then it proceeds to score the round at that point, taking the last element.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.4.improved</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.set <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>subset? difference<span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-edn<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<<vectorize>>
<<won>>
<<Game>>
<<play-round>>
<<make-game>>
<<score-winners>>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #bbb;">[</span>games <span style="color: #494949;">(</span>iterate play-round <span style="color: #bbb;">(</span>make-game <span style="color: #494949;">"./other/advent-of-code-2021-04_input.edn"</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">[</span>#<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">when</span> <span style="color: #494949;">(</span>empty? <span style="color: #bbb;">(</span><span style="color: #E53935;">:boards</span> <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span> <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span> #<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">when</span> <span style="color: #494949;">(</span>not <span style="color: #bbb;">(</span>empty? <span style="color: #494949;">(</span><span style="color: #E53935;">:winners</span> <span style="font-style: italic;">%</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span> <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>map #<span style="color: #bbb;">(</span>last <span style="color: #494949;">(</span>score-winners <span style="color: #bbb;">(</span>some <span style="font-style: italic;">%</span> games<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="outline-container-orgd0cc6f8" class="outline-2">
<h2 id="orgd0cc6f8">Day 5: Hydrothermal Venture</h2>
<div class="outline-text-2" id="text-orgd0cc6f8">
<p>
This next problem asks us to find the intersections of various lines. To do this we.
</p>
</div>
<div id="outline-container-org7c98c5c" class="outline-3">
<h3 id="org7c98c5c">Common</h3>
<div class="outline-text-3" id="text-org7c98c5c">
</div>
<div id="outline-container-orgff1d94f" class="outline-4">
<h4 id="orgff1d94f">draw-line-segment</h4>
<div class="outline-text-4" id="text-orgff1d94f">
<p>
Here we introduce a simple function that produces a set of integer or rational coordinates at the natural numbers on the line segment provided by the two coordinates given to the function in a tuple.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="orgbd4f4aa"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">draw-line-segment</span> <span style="color: #bbb;">[</span><span style="color: #494949;">[</span><span style="color: #bbb;">[</span>x1 y1<span style="color: #bbb;">]</span> <span style="color: #bbb;">[</span>x2 y2<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>y-diff <span style="color: #bbb;">(</span>- y1 y2<span style="color: #bbb;">)</span>
x-diff <span style="color: #bbb;">(</span>- x1 x2<span style="color: #bbb;">)</span>
<span style="color: #bbb;">[</span>xmin xmax<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span>sort <span style="color: #494949;">[</span>x1 x2<span style="color: #494949;">]</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">[</span>ymin ymax<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span>sort <span style="color: #494949;">[</span>y1 y2<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">cond</span> <span style="color: #bbb;">(</span>zero? x-diff<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">(</span>range ymin <span style="color: #bbb;">(</span>inc ymax<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span>y<span style="color: #494949;">]</span> <span style="color: #494949;">[</span>x1 y<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
set<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>zero? y-diff<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">(</span>range xmin <span style="color: #bbb;">(</span>inc xmax<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span>x<span style="color: #494949;">]</span> <span style="color: #494949;">[</span>x y1<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
set<span style="color: #bbb;">)</span>
<span style="color: #E53935;">:else</span> <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">let</span> <span style="color: #494949;">[</span>m <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>zero? y-diff<span style="color: #494949;">)</span> 0 <span style="color: #494949;">(</span>/ x-diff y-diff<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
b <span style="color: #bbb;">(</span>- y1 <span style="color: #494949;">(</span>* m x1<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #bbb;">(</span>range xmin <span style="color: #494949;">(</span>inc xmax<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>map <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span>x<span style="color: #bbb;">]</span> <span style="color: #bbb;">[</span>x <span style="color: #494949;">(</span>+ <span style="color: #bbb;">(</span>* x m<span style="color: #bbb;">)</span> b<span style="color: #494949;">)</span><span style="color: #bbb;">]</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
set<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org3db266e" class="outline-4">
<h4 id="org3db266e">find-intersections</h4>
<div class="outline-text-4" id="text-org3db266e">
<p>
To find the points on which each one intersects we simply consider each point and find the set intersections of it with the rest of the sets. We then (to avoid double-counting) discard the set from the set of coordinates and continue on this new set until all are exhausted.
</p>
<div class="org-src-container">
<pre class="src src-clojure" id="org9a23618"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">find-intersections</span> <span style="color: #bbb;">[</span><span style="color: #494949;">[</span>f & rest<span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>empty? rest<span style="color: #494949;">)</span>
#<span style="color: #494949;">{}</span>
<span style="color: #494949;">(</span>apply union
<span style="color: #bbb;">(</span>find-intersections rest<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>map <span style="color: #494949;">(</span>partial intersection f<span style="color: #494949;">)</span> rest<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org1d9a6b0" class="outline-4">
<h4 id="org1d9a6b0">parse-lines</h4>
<div class="outline-text-4" id="text-org1d9a6b0">
<p>
Here we simply parse the list into a series of tuples containing the coordinates.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">parse-lines</span> <span style="color: #bbb;">[</span>s<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> s
<span style="color: #494949;">(</span><span style="color: #E53935;">str</span>/split-lines<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span>s<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #bbb;">(</span><span style="color: #E53935;">str</span>/split s #<span style="color: #494949;">"->"</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>map #<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #bbb;">(</span><span style="color: #E53935;">str</span>/split <span style="font-style: italic;">%</span> #<span style="color: #494949;">","</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>map read-string<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgdc52816" class="outline-3">
<h3 id="orgdc52816">Part A</h3>
<div class="outline-text-3" id="text-orgdc52816">
<p>
Now to actually execute all we need to do is filter out the unwanted functions, which are in this case are all the functions that are not vertical or horizontal.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.5.a</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.set <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>intersection union<span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-file<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<<draw-line-segment>>
<<find-intersections>>
<<parse-lines>>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">"./other/advent-of-code-2021-05_input.txt"</span>
read-file
parse-lines
<span style="color: #bbb;">(</span>filter <span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #bbb;">[</span><span style="color: #494949;">[</span><span style="color: #bbb;">[</span>x1 y1<span style="color: #bbb;">]</span> <span style="color: #bbb;">[</span>x2 y2<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">or</span> <span style="color: #494949;">(</span>= x1 x2<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>= y1 y2<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>map draw-line-segment<span style="color: #bbb;">)</span>
find-intersections
count<span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org84aad6e" class="outline-3">
<h3 id="org84aad6e">Part B</h3>
<div class="outline-text-3" id="text-org84aad6e">
<p>
For this next part we simply remove the filter we used in the last one and let it run again.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.5.b</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.set <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>intersection union<span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-file<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<<draw-line-segment>>
<<find-intersections>>
<<parse-lines>>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">"./other/advent-of-code-2021-05_input.txt"</span>
read-file
parse-lines
<span style="color: #bbb;">(</span>map draw-line-segment<span style="color: #bbb;">)</span>
find-intersections
count<span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orga0adb3a" class="outline-2">
<h2 id="orga0adb3a">Day 6: Lanternfish</h2>
<div class="outline-text-2" id="text-orga0adb3a">
</div>
<div id="outline-container-orga1c3434" class="outline-3">
<h3 id="orga1c3434">Part A</h3>
<div class="outline-text-3" id="text-orga1c3434">
<p>
Here we basically implement what we can imagine as an entity component system with some basic rules. Each entity in the system has a reproduction time (<code>rep-time</code>) which, for each update of the system, is decremented by one. Should an entity in the system have a <code>rep-time</code> of less than 0 it will create a new entity with a <code>rep-time</code> of 8 and have it's <code>rep-time</code> set to 6.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.6.a</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span>
<span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-edn<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">age</span> <span style="color: #bbb;">[</span>rep-time<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>dec rep-time<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">reproduce</span> <span style="color: #bbb;">[</span>rep-time<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>< rep-time 0<span style="color: #494949;">)</span>
<span style="color: #494949;">[</span>6 8<span style="color: #494949;">]</span>
rep-time<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">age-day</span> <span style="color: #bbb;">[</span>fish-list<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> fish-list
<span style="color: #494949;">(</span>map age<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map reproduce<span style="color: #494949;">)</span>
flatten<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">sim-n-days</span> <span style="color: #bbb;">[</span>n initial-state<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> initial-state
<span style="color: #494949;">(</span>iterate age-day<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>take <span style="color: #bbb;">(</span>inc n<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
last<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>count <span style="color: #bbb;">(</span>sim-n-days 80 <span style="color: #494949;">(</span>read-edn <span style="color: #494949;">"./other/advent-of-code-2021-06_input.edn"</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
</colgroup>
<tbody>
<tr>
<td class="org-left">#'aoc.6.a/reproduce</td>
</tr>
<tr>
<td class="org-left">#'aoc.6.a/age-day</td>
</tr>
<tr>
<td class="org-left">#'aoc.6.a/sim-n-days</td>
</tr>
<tr>
<td class="org-left">389726</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-orgea6b53b" class="outline-3">
<h3 id="orgea6b53b">Part B</h3>
<div class="outline-text-3" id="text-orgea6b53b">
<p>
Here we take a dramatically improved approach by, instead of treating each fish as one number, simply counting the number of fish with a certain time to reproduce. Therefore, each lantern fish is not recorded separately.
</p>
<p>
Each time we update we simply shift all the values to the lower day number, adding in the case of day 7 being shifted to day 6.
</p>
<p>
When a set of lantern fish have reached zero we place them back at 6 and we add the same number to day 8.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.6.b</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>aoc.util <span style="color: #E53935;">:refer</span> <span style="color: #bbb;">[</span>read-edn<span style="color: #bbb;">]</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">age-and-reproduce</span> <span style="color: #bbb;">[</span><span style="color: #494949;">[</span>n0 n1 n2 n3 n4 n5 n6 n7 n8<span style="color: #494949;">]</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">[</span>n1 n2 n3 n4 n5 n6 <span style="color: #494949;">(</span>+ n0 n7<span style="color: #494949;">)</span> n8 n0<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">load-lanternfish</span> <span style="color: #bbb;">[</span>fish<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>reduce
#<span style="color: #494949;">(</span>update <span style="font-style: italic;">%1</span> <span style="font-style: italic;">%2</span> inc<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>vec <span style="color: #bbb;">(</span>repeat 9 0<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
fish<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">sim-n-days</span> <span style="color: #bbb;">[</span>n fish<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> fish
load-lanternfish
<span style="color: #494949;">(</span>iterate age-and-reproduce<span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>take <span style="color: #bbb;">(</span>inc n<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
last
<span style="color: #494949;">(</span>reduce +<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>sim-n-days 256 <span style="color: #bbb;">(</span>read-edn <span style="color: #494949;">"./other/advent-of-code-2021-06_input.edn"</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org1f5ef0b" class="outline-2">
<h2 id="org1f5ef0b">Day 7: The Treachery of Whales</h2>
<div class="outline-text-2" id="text-org1f5ef0b">
<p>
Here we get into a simple optimization problem where we just have to find the most fuel-efficient position for all crabs. The algorithm I implemented tests all possible positions, giving us and \(O(n^2)\) runtime, though I think that could be improved a fair bit
</p>
</div>
<div id="outline-container-org355ad4d" class="outline-3">
<h3 id="org355ad4d">Part A</h3>
<div class="outline-text-3" id="text-org355ad4d">
<p>
Here we simply brute-force the answer.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.7.a</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>clojure.java.io <span style="color: #E53935;">:as</span> io<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">abs</span> <span style="color: #bbb;">[</span>n<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>< n 0<span style="color: #494949;">)</span> <span style="color: #494949;">(</span>- n<span style="color: #494949;">)</span> n<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">optimize-crabs</span> <span style="color: #bbb;">[</span>crabs<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">(</span>range <span style="color: #bbb;">(</span>apply min crabs<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>apply max crabs<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span>dist<span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>dist <span style="color: #bbb;">(</span>reduce + <span style="color: #494949;">(</span>map #<span style="color: #bbb;">(</span>abs <span style="color: #494949;">(</span>- <span style="font-style: italic;">%</span> dist<span style="color: #494949;">)</span><span style="color: #bbb;">)</span> crabs<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>reduce <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span><span style="color: #bbb;">[</span>adist afuel<span style="color: #bbb;">]</span> <span style="color: #bbb;">[</span>dist fuel<span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span><= fuel afuel<span style="color: #bbb;">)</span>
<span style="color: #bbb;">[</span>dist fuel<span style="color: #bbb;">]</span>
<span style="color: #bbb;">[</span>adist afuel<span style="color: #bbb;">]</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>optimize-crabs <span style="color: #bbb;">(</span>map read-string
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> <span style="color: #494949;">"./other/advent-of-code-2021-07_input.txt"</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">io</span>/resource<span style="color: #bbb;">)</span>
slurp
<span style="color: #bbb;">(</span><span style="color: #E53935;">str</span>/split #<span style="color: #494949;">","</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org768213e" class="outline-3">
<h3 id="org768213e">Part B</h3>
<div class="outline-text-3" id="text-org768213e">
<p>
Now on part b we have the fuel increase with the range, so we make a slight modification.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.7.a</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.string <span style="color: #E53935;">:as</span> str<span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>clojure.java.io <span style="color: #E53935;">:as</span> io<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">abs</span> <span style="color: #bbb;">[</span>n<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #494949;">(</span>< n 0<span style="color: #494949;">)</span> <span style="color: #494949;">(</span>- n<span style="color: #494949;">)</span> n<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">optimize-crabs</span> <span style="color: #bbb;">[</span>crabs<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">(</span>range <span style="color: #bbb;">(</span>apply min crabs<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>apply max crabs<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>map <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span>dist<span style="color: #494949;">]</span>
<span style="color: #494949;">[</span>dist <span style="color: #bbb;">(</span>reduce + <span style="color: #494949;">(</span>map #<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> <span style="color: #494949;">(</span>- <span style="font-style: italic;">%</span> dist<span style="color: #494949;">)</span> abs inc range <span style="color: #494949;">(</span>reduce +<span style="color: #494949;">)</span><span style="color: #bbb;">)</span> crabs<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>reduce <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">fn</span> <span style="color: #494949;">[</span><span style="color: #bbb;">[</span>adist afuel<span style="color: #bbb;">]</span> <span style="color: #bbb;">[</span>dist fuel<span style="color: #bbb;">]</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">if</span> <span style="color: #bbb;">(</span><= fuel afuel<span style="color: #bbb;">)</span>
<span style="color: #bbb;">[</span>dist fuel<span style="color: #bbb;">]</span>
<span style="color: #bbb;">[</span>adist afuel<span style="color: #bbb;">]</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>optimize-crabs <span style="color: #bbb;">(</span>map read-string
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">-></span> <span style="color: #494949;">"./other/advent-of-code-2021-07_input.txt"</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">io</span>/resource<span style="color: #bbb;">)</span>
slurp
<span style="color: #bbb;">(</span><span style="color: #E53935;">str</span>/split #<span style="color: #494949;">","</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org1da0441" class="outline-2">
<h2 id="org1da0441"><span class="todo TODO">TODO</span> Day 8: Seven Segment Search</h2>
<div class="outline-text-2" id="text-org1da0441">
<p>
Here we break out my favorite tool, logic programming<label class="sidenote-number" for="6"><sup>6</sup></label><input checked="checked" id="6" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 6</span> Basically it's the class of programming that contains GOFAI.</span>. More specifically, we need to use backwards chaining to find the collection of facts about the translation connections that produce the results we see here.
</p>
<p>
So let's imagine what we want first. What we want is a translation table that tells us what the inputs should be from the outputs and vise-versa. Something akin to this translation table.<label class="sidenote-number" for="7"><sup>7</sup></label><input checked="checked" id="7" style="display:none" type="checkbox" /><span class="sidenote"><span class="sidenote-number"> 7</span> Or, if you like diagrams: <img src="./images/trans-table_edc90865-ee8f-4e91-a685-620a2dd6438b.png" alt="trans-table_edc90865-ee8f-4e91-a685-620a2dd6438b.png" /></span>
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">trans-table</span> <span style="color: #bbb;">{</span>1 2
2 6
3 5
4 4
5 3
6 1<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
This provides us with an obvious starting point. We also know that a letter will correspond to the same letter in each round.
</p>
</div>
<div id="outline-container-orga6d25a7" class="outline-3">
<h3 id="orga6d25a7">Part A</h3>
<div class="outline-text-3" id="text-orga6d25a7">
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">aoc.8.a</span>
<span style="color: #bbb;">(</span><span style="color: #E53935;">:require</span> <span style="color: #494949;">[</span>clojure.core.logic <span style="color: #E53935;">:as</span> logic<span style="color: #494949;">]</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-0</span> '#<span style="color: #bbb;">{</span>a b c e f g<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-1</span> '#<span style="color: #bbb;">{</span>c f<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-2</span> '#<span style="color: #bbb;">{</span>a c d e g<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-3</span> '#<span style="color: #bbb;">{</span>a c d e g<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-4</span> '#<span style="color: #bbb;">{</span>b c d f<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-5</span> '#<span style="color: #bbb;">{</span>a b d f g<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-6</span> '#<span style="color: #bbb;">{</span>a b d e f g<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-7</span> '#<span style="color: #bbb;">{</span>a c f<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-8</span> '#<span style="color: #bbb;">{</span>a b c d e f g<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">def</span> <span style="font-style: italic;">disp-9</span> '#<span style="color: #bbb;">{</span>a b c d f g<span style="color: #bbb;">}</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org82a88a0" class="outline-3">
<h3 id="org82a88a0">Part B</h3>
</div>
</div>
<!-- Footnotes --><!--
<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">List destructuring is really one of my favorite bits of Clojure's syntax. Yes, it's common, but it's also really useful.</p></div></div>
<div class="footdef"><sup><a id="fn.2" class="footnum" href="#fnr.2" role="doc-backlink">2</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">I think this is reasonably within the rules. The lists are basically structured so that I only have to add an extra pair of square brackets to make it a vector.</p></div></div>
<div class="footdef"><sup><a id="fn.3" class="footnum" href="#fnr.3" role="doc-backlink">3</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">See recursion.</p></div></div>
<div class="footdef"><sup><a id="fn.4" class="footnum" href="#fnr.4" role="doc-backlink">4</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">Also known as a test function.</p></div></div>
<div class="footdef"><sup><a id="fn.5" class="footnum" href="#fnr.5" role="doc-backlink">5</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">This is only used once, but having a function makes what I am doing clearer.</p></div></div>
<div class="footdef"><sup><a id="fn.6" class="footnum" href="#fnr.6" role="doc-backlink">6</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">Basically it's the class of programming that contains GOFAI.</p></div></div>
<div class="footdef"><sup><a id="fn.7" class="footnum" href="#fnr.7" role="doc-backlink">7</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">Or, if you like diagrams: <img src="./images/trans-table_edc90865-ee8f-4e91-a685-620a2dd6438b.png" alt="trans-table_edc90865-ee8f-4e91-a685-620a2dd6438b.png" /></p></div></div>