Powered by AppSignal & Oban Pro

Day One: Dusty Shed

2026/1.livemd

Day One: Dusty Shed

Mix.install([
  {:kino, "~> 0.19.0"}
])

Problem

https://adventofcode.com/2026/day/1

defmodule Problem do
  defmacro __using__(_opts \\ []) do
    quote do
      import Kernel, except: [inspect: 1, inspect: 2]
      import unquote(__MODULE__)
    end
  end

  def inspect(term, opts \\ []) do
    Kernel.inspect(term, Keyword.merge([
      pretty: true,
      limit: :infinity,
      printable_limit: :infinity,
      structs: false,
      charlists: :as_lists,
      custom_options: [sort_maps: true]
    ], opts))
  end

  def parse(string) when is_binary(string) do
    string |> String.split() |> Stream.map(fn string ->
      {integer, ""} = Integer.parse(string)
      integer
    end)
  end

  def frequencies(stream, top_n, order) do
    stream
    |> Enum.frequencies()
    |> Enum.sort_by(fn {_integer, frequency} -> frequency end, order)
    |> Enum.take(top_n)
    |> Enum.map(fn {integer, _frequency} -> integer end)
  end
end

Part One

"So, you know how we've been having such bad weather?" The Elf's hat is almost blown off her head as she leads you further into the outskirts of the North Pole complex. "The planes haven't been able to fly for a while, which means we're getting quite low on supplies. If we don't get new parts soon, we won't have time to make all the presents for Christmas!"

She leads you around a corner and toward a building you've never been inside. "Of course, we haven't always used planes." You almost trip over a long piece of metal as she slowly pushes open a rusty door on the side of the building. It takes your vision a few moments to adjust to the darkness; at first, all you can make out is dust and cobwebs.

"That brings us to our backup plan." You finally manage to resolve the large machine in the middle of the room: it's a giant Christmas train! Elves are hurriedly cleaning, organizing, loading, and otherwise preparing the train for immediate departure.

The Elf carefully leads you through the chaos. "There's an old rail network that reaches enough of the important facilities near here to get us what we need, if you're quick." Clearly, the Elves have already decided that you will be going on this mission.

To save Christmas, the Elves need you to return with the supplies they need by December 12th.

Collect stars by solving puzzles. Two puzzles will be made available on each day; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

There are only six more types of things that the Elves need to find and load on the train before it can depart. All of the crates, tanks, boxes, and bins are labeled with the ID number of the type of thing they contain; the Elves have even found a list of each container's ID number (your puzzle input) for you to reference.

Unfortunately, although there was clearly a careful inventory management system at work here once, those records seem to have been lost, and so nobody remembers what each ID actually means.

You approach a group of Elves searching for the contraptions, gizmos, and apparatuses. They have had no luck searching through the hundreds of crates, but one of the Elves seems to recall that they used to have a lot of apparatuses and gizmos and contraptions.

Maybe there's a faster way to find what they're looking for: identify the three most common IDs in the list. For example:

57
57
55
57
76
78
57
45
43
78
43
76
43
78
57
45
57
76
43
43
76

In this list, the three most common IDs are 76, 57, and 43. Surely, these are the IDs for the gizmos, contraptions, and apparatuses! (Multiplying these IDs together gives 186276.)

Analyze your list of IDs. What do you get if you multiply together the three IDs which appear most often?

Sample Input

sample_input = """
57
57
55
57
76
78
57
45
43
78
43
76
43
78
57
45
57
76
43
43
76
"""

Solution One

defmodule Problem.One do
  use Problem

  def solve(parsed) do
    parsed |> frequencies(3, :desc) |> Enum.product()
  end
end

Actual Input

actual_input = """
158
842
595
492
518
990
386
877
328
666
336
356
512
727
232
282
829
344
757
366
659
670
525
310
863
285
543
616
510
831
283
866
552
100
809
771
328
942
591
907
821
466
926
338
394
588
285
559
438
846
505
786
122
893
903
490
809
620
356
867
668
342
190
175
588
150
616
578
235
133
868
262
258
564
840
360
371
982
745
695
146
821
164
225
748
159
862
130
729
212
242
329
747
692
351
592
353
798
979
591
289
357
856
965
722
918
869
488
190
310
918
227
527
296
291
616
713
387
345
931
710
286
930
414
866
620
329
737
937
429
363
344
195
556
377
614
857
730
486
310
567
626
444
378
606
101
715
301
884
593
126
846
222
412
851
507
392
360
578
336
893
295
486
862
698
406
138
214
981
740
586
786
226
410
840
525
883
559
348
774
109
479
333
873
501
191
291
227
221
452
331
320
367
119
541
250
626
890
449
713
846
937
325
276
419
821
810
914
617
187
405
921
109
410
798
329
806
504
480
889
392
488
166
731
841
158
393
631
988
139
471
782
849
837
376
682
286
184
548
952
829
253
835
188
670
285
254
263
751
602
237
182
418
604
666
433
637
232
785
839
582
747
346
496
636
786
278
439
453
697
479
810
843
319
758
747
855
418
328
328
626
855
291
235
278
161
709
775
835
385
601
147
319
246
436
545
586
631
486
377
626
528
250
366
262
313
979
444
702
620
876
352
962
455
628
545
455
295
445
130
591
661
777
762
649
323
573
521
593
695
747
871
107
809
839
467
467
902
880
533
498
681
436
500
708
817
867
957
376
471
192
306
101
149
530
121
775
988
520
259
371
713
759
718
791
191
697
122
985
377
116
550
202
351
120
538
899
463
464
599
722
490
805
669
165
192
605
558
584
480
133
263
480
967
685
339
464
403
900
244
344
608
138
403
704
400
703
950
874
604
264
340
661
175
912
668
159
856
400
837
624
559
584
203
170
860
635
257
738
449
501
747
383
331
905
147
494
255
385
874
700
947
898
348
520
912
869
593
640
876
682
636
161
659
779
477
478
692
262
709
557
815
320
634
539
953
555
406
222
803
163
904
806
985
740
394
357
573
303
798
295
827
369
667
541
941
681
223
303
857
902
687
408
198
545
170
659
806
622
657
439
481
795
605
548
981
954
767
771
635
421
410
368
699
363
758
883
257
828
592
452
183
541
438
942
371
606
976
726
335
699
719
573
368
724
847
799
147
652
990
737
211
937
492
883
851
183
280
574
950
521
296
749
772
196
325
674
276
667
745
495
801
860
981
827
890
146
538
998
921
823
559
195
529
982
339
433
635
165
669
738
386
163
787
582
762
739
919
195
251
637
139
719
289
588
829
661
191
627
390
668
174
844
604
617
748
392
731
149
699
793
378
604
293
952
436
513
227
548
876
393
466
453
606
386
904
954
198
970
809
448
586
765
335
282
601
202
320
529
504
785
123
380
643
711
979
958
850
625
394
874
205
880
641
627
816
333
344
596
390
728
626
270
643
308
213
165
376
307
693
236
110
146
736
183
478
817
602
948
702
404
482
989
246
556
731
114
530
146
682
150
383
455
533
530
392
513
965
763
413
539
156
364
255
348
544
101
620
357
539
504
607
280
599
627
336
868
221
400
471
943
900
278
843
876
687
564
164
953
192
378
763
976
839
291
162
333
527
232
815
728
777
444
255
786
668
726
608
467
701
257
163
245
758
715
829
655
312
918
889
853
270
641
250
113
704
730
840
303
667
703
864
183
329
582
141
592
205
140
162
702
708
810
101
889
989
550
771
760
242
643
419
178
924
669
746
302
914
856
856
927
552
839
805
116
500
963
870
748
148
159
793
319
681
982
907
851
808
356
262
325
416
652
570
623
307
753
527
313
196
386
308
299
999
595
711
194
412
957
757
285
838
463
906
853
388
126
634
520
815
775
160
171
898
963
842
323
831
906
214
413
988
914
620
345
739
938
704
608
282
889
739
109
577
736
563
338
559
496
938
213
439
175
232
627
985
254
221
591
713
924
412
635
280
429
605
838
698
258
291
884
107
187
174
162
949
245
965
638
307
759
914
862
536
835
893
754
657
957
368
438
926
457
367
230
864
985
746
445
363
596
779
563
246
884
299
211
243
545
181
749
140
301
762
795
335
230
357
426
528
589
760
623
763
405
121
628
570
709
368
232
564
649
893
912
942
645
941
340
599
504
150
464
486
957
772
418
950
414
942
898
326
867
637
749
837
236
501
805
387
771
479
473
181
649
681
312
589
171
782
126
340
198
850
726
641
645
758
160
574
948
159
649
164
465
114
668
250
902
659
130
821
799
837
243
513
887
988
190
693
692
963
820
931
779
367
871
640
199
729
352
457
188
645
563
595
377
794
574
302
550
259
847
563
110
342
495
248
298
251
551
933
849
351
392
498
254
966
460
101
729
529
352
617
222
416
166
699
628
353
943
527
530
999
891
463
181
598
953
702
889
543
538
406
184
728
873
634
350
577
156
567
310
785
461
692
924
737
709
545
898
827
927
529
110
194
408
809
113
905
880
753
146
967
191
149
539
342
289
820
481
931
426
100
403
364
903
981
466
528
787
479
187
116
727
418
351
727
844
999
158
242
574
847
270
194
891
623
753
280
251
194
446
815
280
445
670
205
667
938
226
242
941
791
943
364
989
905
649
774
625
669
954
886
225
150
171
416
643
431
998
624
194
774
933
433
958
406
899
777
139
156
498
416
584
719
350
609
525
226
703
510
388
395
963
490
772
414
465
628
686
871
395
303
617
202
307
431
544
140
587
481
212
866
477
966
740
835
182
293
853
708
943
162
831
868
918
847
263
577
342
607
971
645
929
909
843
460
214
255
496
701
254
616
697
141
556
213
404
140
236
507
570
505
605
900
313
543
970
592
652
772
338
767
212
598
890
998
810
608
335
120
122
378
765
538
319
492
745
264
378
529
734
737
245
794
162
278
465
264
869
184
871
371
191
643
801
739
695
286
841
276
893
306
636
703
492
631
762
221
345
628
779
907
353
700
702
771
237
413
182
405
844
601
263
123
754
438
921
757
863
718
119
481
988
864
251
938
877
505
258
360
141
264
393
843
532
779
731
510
301
388
719
765
164
783
255
897
123
891
641
745
687
460
584
761
289
904
909
874
740
817
905
574
674
166
850
870
933
634
860
870
794
782
787
368
302
211
880
637
661
625
698
949
518
170
431
722
158
828
116
400
918
555
787
710
391
839
886
607
120
439
926
573
258
383
429
558
693
184
592
114
286
835
421
602
356
413
614
148
838
746
903
595
634
919
165
846
369
477
849
947
248
295
270
119
759
494
719
163
405
904
345
850
473
843
501
943
904
853
962
674
900
226
710
452
391
655
181
867
461
924
734
198
638
669
520
816
926
929
404
899
120
366
174
248
701
708
192
148
412
578
246
555
606
262
473
404
533
386
436
710
552
989
336
446
587
929
385
360
489
941
851
100
636
622
257
149
624
700
130
877
480
587
528
772
453
419
203
667
281
171
388
473
109
433
682
754
841
685
555
976
606
325
302
281
501
810
558
801
230
942
998
609
429
625
460
636
178
803
767
736
857
962
687
622
831
120
563
235
971
282
828
782
339
539
686
544
605
844
827
195
849
856
253
270
436
551
418
494
439
652
760
753
141
380
749
175
907
587
306
701
467
838
912
877
445
254
455
616
170
156
903
144
736
897
850
761
276
199
551
461
745
948
296
225
655
887
732
775
243
114
748
827
599
144
408
276
457
479
390
532
793
738
952
949
762
281
930
567
175
759
461
578
130
161
466
685
863
729
121
298
838
808
748
338
426
899
927
164
342
495
326
976
536
363
212
416
593
907
369
293
897
761
835
816
855
507
521
749
971
307
871
298
265
933
536
222
557
598
299
376
383
957
567
196
926
465
990
692
899
552
698
874
708
464
686
685
367
346
631
873
903
174
614
798
631
395
573
799
947
842
794
138
331
306
448
724
351
557
471
199
404
457
990
326
962
960
236
449
133
258
635
887
495
751
199
518
406
387
958
701
666
265
253
933
237
622
950
828
970
809
815
591
728
338
570
548
243
203
344
864
890
730
205
488
589
119
323
758
799
188
100
831
285
394
921
795
919
278
953
698
144
855
246
243
376
963
223
853
595
640
711
732
174
709
829
336
113
543
803
161
126
471
791
623
751
405
954
820
453
960
921
421
198
160
496
846
122
719
188
532
909
761
687
107
308
601
110
482
496
931
625
301
929
886
985
584
783
325
960
737
699
738
214
533
715
244
213
806
429
500
608
929
782
259
298
380
967
783
222
244
446
223
518
282
448
532
724
463
609
448
763
244
182
841
912
192
346
624
685
682
367
609
931
617
482
391
433
657
431
732
774
835
622
638
187
230
823
793
864
293
340
763
310
489
924
339
704
453
670
823
930
760
301
452
555
244
489
808
759
150
950
710
346
803
842
532
478
734
700
760
731
486
265
230
303
312
739
550
350
641
178
937
295
906
805
557
966
299
999
596
970
"""

Part Two

That worked perfectly! The Elves quickly locate all the contraptions, apparatuses, and gizmos they need.

While they're loaded onto the train, you go to help the Elves looking for the remaining three types of things they need: thingamabobs, thingamajigs, and screws. One of the Elves vaguely recalls her grandfather complaining about how he had to walk all the way across the shed every time he needed a thingamajig, thingamabob, or a screw, since there were always so few containers of each.

This time, you'll need to identify the three least common IDs in the list. In the example list above, the three least common IDs are 45, 78, and 55; these multiply together to produce 193050.

Analyze your list of IDs again. What do you get if you multiply together the three IDs which appear least often?

Solution Two

defmodule Problem.Two do
  use Problem

  def solve(parsed) do
    parsed |> frequencies(3, :asc) |> Enum.product()
  end
end

Answers

 form =
  Kino.Control.form(
    [
      problem: Kino.Input.select("Problem", [
        one: "Problem One",
        two: "Problem Two"
      ]),
      input: Kino.Input.select("Input", [
        sample_input: "Sample Input",
        actual_input: "Actual Input"
      ])
    ],
    submit: "Solve"
  ) |> Kino.render

frame = Kino.Frame.new() |> Kino.render()

Kino.listen(form, fn event ->
  use Problem

  {problem, input} = case {event.data.problem, event.data.input} do
    {:one, :sample_input} -> {Problem.One, sample_input}
    {:one, :actual_input} -> {Problem.One, actual_input}
    {:two, :sample_input} -> {Problem.Two, sample_input}
    {:two, :actual_input} -> {Problem.Two, actual_input}
  end

  result = input
    |> Problem.parse()
    |> problem.solve()
    |> inspect()
    |> Kino.Text.new(terminal: false)

  Kino.Frame.clear(frame)
  Kino.Frame.append(frame, result)
end)