Day 13
Setup
Mix.install([
{:kino, "~> 0.4.1"}
])
Example input:
6,10
0,14
9,10
0,3
10,4
4,11
6,0
6,12
4,1
0,13
10,12
3,4
3,0
8,4
1,10
2,14
8,10
9,0
fold along y=7
fold along x=5
Real input:
1141,761
175,114
628,854
654,506
314,266
370,883
642,826
244,669
739,429
970,852
1298,740
1121,777
642,579
497,308
1150,460
644,60
745,866
1169,882
807,338
319,705
740,647
763,632
1163,829
1278,408
460,675
1002,301
162,550
243,353
858,240
632,372
704,749
604,494
917,240
1034,838
261,800
585,238
741,39
315,674
1298,803
189,117
606,184
797,348
244,144
1290,110
1198,54
459,192
401,868
70,217
1077,54
634,110
982,660
179,737
1221,5
49,810
223,423
760,600
170,42
679,380
658,793
78,625
1036,283
995,674
353,225
1032,525
735,353
140,372
333,830
609,410
900,213
154,817
218,667
726,217
258,268
502,889
654,424
237,677
499,870
995,686
1265,238
112,795
32,30
1230,682
734,625
539,208
353,221
1019,292
324,236
137,22
1148,176
44,320
482,350
761,95
276,838
1266,320
172,749
976,194
480,584
403,144
172,593
1044,283
406,255
150,0
545,488
195,416
1299,226
140,820
569,855
345,290
962,723
35,816
426,542
524,103
92,392
587,336
358,235
321,474
666,39
1115,478
479,444
15,441
79,317
112,54
62,313
972,248
524,299
1096,270
851,702
1262,184
554,856
363,78
850,637
87,592
152,5
1153,581
185,98
1135,114
1231,317
398,390
524,567
68,402
398,838
108,756
216,78
425,256
1029,500
1099,763
20,784
410,681
544,567
636,166
196,103
971,208
821,432
1180,810
893,553
1146,12
223,726
141,418
340,467
269,35
195,478
572,254
132,616
62,718
472,525
656,449
611,894
1098,110
1089,799
18,276
258,402
1136,423
1158,808
324,460
89,341
308,288
334,700
1221,30
634,499
517,565
763,665
314,822
11,226
1091,184
142,171
644,453
537,298
62,176
684,16
294,38
179,1
1160,894
1174,644
1180,82
776,520
1173,872
744,348
1057,196
1298,826
1150,152
950,455
1300,60
460,616
932,614
984,22
252,331
455,372
922,199
88,409
564,631
970,315
182,2
572,634
408,234
632,624
1221,530
48,184
401,26
1014,464
1128,444
1233,290
446,521
47,880
238,369
407,465
1006,423
1087,23
1009,511
723,663
1278,439
867,131
1009,665
1198,99
97,61
539,238
1153,26
642,875
141,882
32,590
705,138
343,453
1180,586
340,180
70,504
681,872
408,820
652,182
1158,310
1098,784
73,840
1116,21
348,483
756,515
23,514
1028,460
1169,12
1292,276
554,515
850,271
50,481
455,66
674,584
940,883
813,586
454,227
718,46
2,854
1032,822
1087,471
1046,304
1180,308
136,698
584,108
796,731
244,449
781,714
726,677
278,749
22,522
421,432
996,822
1134,178
345,738
913,726
413,66
1213,61
182,749
353,847
738,260
1059,368
12,154
1058,331
1046,269
937,36
765,854
169,26
1228,549
682,40
875,632
223,471
874,530
1026,883
996,427
194,257
952,847
900,640
880,200
8,151
1262,145
546,152
1012,729
955,9
1282,873
1016,663
850,392
725,656
139,523
592,189
934,525
1168,171
412,423
1057,474
299,457
388,361
127,828
828,145
1041,35
909,478
776,374
1094,791
398,120
502,23
783,527
1140,42
1180,327
80,660
376,749
1086,240
994,23
276,280
716,247
1202,756
406,471
282,96
967,441
1015,864
67,663
472,640
166,658
507,513
28,21
1195,686
1192,863
848,148
330,199
453,714
130,586
1072,185
150,296
238,33
989,420
1278,887
281,52
338,248
763,229
283,218
738,449
575,353
1169,418
957,221
658,854
597,351
721,352
723,336
964,86
857,712
155,306
78,191
469,12
1243,663
162,718
281,500
975,306
393,58
637,828
80,522
246,717
308,606
229,256
462,820
1138,593
830,584
950,142
495,711
852,187
130,362
147,289
1228,345
671,673
1077,432
1064,32
684,878
960,148
984,458
32,439
1141,313
264,269
257,616
229,638
1290,784
718,257
1201,40
314,803
458,856
1305,382
658,773
995,220
321,469
1202,667
850,278
920,403
1124,494
808,711
989,425
927,856
1178,891
594,199
897,150
462,522
331,184
1230,1
601,70
150,436
241,150
994,616
932,280
226,248
10,386
1184,3
196,327
388,199
169,133
745,476
460,392
1173,777
390,624
939,560
855,66
360,142
1096,504
529,462
479,562
932,736
155,446
234,724
277,308
1225,514
224,654
745,478
38,21
581,812
89,364
770,570
62,189
970,266
991,189
864,521
806,296
494,12
488,152
333,64
489,208
130,327
1155,870
84,647
1014,399
975,474
1223,512
184,134
1054,665
831,444
1016,38
678,522
1078,721
1009,217
406,647
90,77
726,786
547,297
201,462
296,430
436,227
109,40
170,465
663,588
957,225
594,647
1185,740
654,449
656,470
1265,208
566,348
363,623
1191,387
676,395
749,450
1114,595
1029,52
1232,255
735,541
79,129
223,23
639,889
152,808
783,197
211,315
745,418
831,450
524,595
619,826
1135,780
315,686
515,371
415,64
746,631
584,217
281,394
1140,465
658,121
678,372
110,359
636,614
806,514
976,602
1067,541
176,716
977,64
565,866
606,749
1136,584
107,665
534,520
233,462
460,502
484,877
1163,320
718,332
753,833
725,238
1033,455
346,86
489,432
796,176
1267,222
825,138
1163,737
1308,40
201,656
458,187
592,562
489,462
1174,196
372,544
1170,372
196,366
266,695
1173,341
1193,298
10,229
88,537
295,30
904,647
132,425
569,156
782,3
176,436
965,290
768,383
877,543
372,350
846,82
125,742
537,148
48,145
783,828
572,445
960,199
1064,673
194,189
1007,66
1222,546
1125,12
1002,512
990,521
380,428
1033,308
1262,301
137,777
1007,514
1216,91
1019,602
920,637
12,91
811,870
678,270
637,66
433,799
311,217
320,521
1148,434
999,217
232,49
164,348
908,563
1158,584
810,525
1123,371
1305,512
688,864
581,474
118,863
115,686
169,138
1202,308
950,612
405,290
709,70
514,74
933,719
182,166
257,278
1014,430
855,25
831,780
517,374
522,469
841,546
1225,738
393,352
1004,171
348,275
321,873
1183,645
826,877
574,269
1275,714
473,156
472,702
766,77
756,422
1298,852
698,201
1028,3
485,868
1265,656
397,726
1173,117
335,420
364,373
1302,151
438,499
502,871
78,177
416,369
745,665
1039,589
793,872
619,154
540,150
147,605
428,198
1169,866
870,105
162,270
781,462
989,469
884,542
478,884
585,656
1131,605
278,374
1087,423
1150,882
334,373
1272,333
970,467
70,56
580,115
69,392
1081,190
1265,110
36,499
996,803
934,749
920,38
112,511
534,374
938,891
1185,152
412,247
676,110
584,675
438,807
1151,183
186,400
673,66
668,292
12,852
574,669
976,373
306,275
390,403
585,315
378,614
576,625
1205,799
350,746
410,445
678,471
549,95
838,192
852,856
319,637
301,511
402,563
339,504
1057,698
62,637
340,315
1076,724
350,563
596,294
771,238
766,499
731,816
278,822
1073,677
562,284
80,372
382,112
57,543
351,66
996,290
1284,406
350,695
1251,726
1029,36
1292,618
517,553
1111,372
584,677
192,372
80,682
808,423
982,458
380,466
199,372
937,708
1128,2
736,625
699,894
897,156
552,311
885,256
1126,134
570,647
458,528
1222,409
725,635
783,380
990,821
1288,423
979,710
502,392
176,290
605,138
557,14
25,673
912,838
1292,584
1298,68
117,298
43,222
967,453
619,740
1062,639
1156,369
544,87
736,269
623,416
5,382
990,292
995,838
649,606
984,872
1051,231
585,635
1298,763
584,56
492,792
686,11
176,604
1275,290
884,654
636,469
147,829
10,646
1300,647
738,254
572,885
507,829
132,54
253,196
373,558
1210,38
504,458
1202,152
194,873
1114,366
632,23
1028,96
89,553
550,600
1068,23
315,838
249,514
897,828
980,695
130,103
401,478
492,434
174,871
416,525
725,315
902,234
196,856
1198,392
587,112
729,474
848,201
609,574
320,821
879,726
1196,598
1016,243
547,665
957,677
12,68
1160,296
1192,339
542,789
524,532
957,453
196,812
726,338
1230,458
642,315
233,483
1180,84
579,176
62,332
246,32
316,229
507,65
691,156
0,470
517,872
644,199
462,201
1155,448
584,838
652,121
314,467
1148,718
893,341
1300,732
818,434
79,577
1222,357
1094,103
619,738
530,449
195,192
311,677
1062,717
28,873
98,206
855,372
726,838
666,199
666,453
972,646
462,74
196,775
1125,98
1248,241
938,350
416,33
1138,301
1114,775
1141,26
1010,194
745,28
816,12
1049,94
291,719
996,490
904,255
704,184
1210,422
184,187
889,462
514,176
783,514
1282,21
130,632
1310,469
786,595
714,152
20,499
989,474
119,387
1016,231
808,392
907,144
390,270
12,292
888,49
248,269
1081,208
1146,98
174,584
798,723
385,859
314,490
1032,374
850,257
915,231
59,168
395,231
182,254
774,443
1073,665
304,695
1288,522
549,799
1248,176
1178,54
725,392
1163,289
1072,369
115,656
622,243
294,231
831,562
169,581
110,395
1186,295
502,616
1222,537
320,73
530,239
110,87
338,487
1009,246
647,868
1067,353
671,221
196,408
632,270
10,647
649,830
1169,236
243,541
1077,483
0,506
1069,828
223,168
1012,654
922,533
766,359
1178,425
185,796
1200,87
1228,254
353,677
50,606
306,171
108,152
1248,313
938,3
1221,364
738,640
813,698
1174,698
425,704
38,469
43,672
1279,416
fold along x=655
fold along y=447
fold along x=327
fold along y=223
fold along x=163
fold along y=111
fold along x=81
fold along y=55
fold along x=40
fold along y=27
fold along y=13
fold along y=6
input = Kino.Input.textarea("Puzzle Input")
Process Input
lines =
input
|> Kino.Input.read()
|> String.split("\n", trim: true)
points =
for line <- lines,
String.match?(line, ~r/^\d/),
do: line |> String.split(",") |> Enum.map(&String.to_integer/1) |> List.to_tuple()
folds =
for line <- lines,
String.match?(line, ~r/^fold/),
do:
line
|> String.split()
|> Enum.at(2)
|> String.split("=")
|> then(fn [axis, value] -> {axis, String.to_integer(value)} end)
:ok
Part 1 first try
[{fold_axis, fold_value} | _] = folds
if fold_axis == "x" do
Enum.map(points, fn {x, y} ->
if x > fold_value, do: {fold_value * 2 - x, y}, else: {x, y}
end)
else
Enum.map(points, fn {x, y} ->
if y > fold_value, do: {x, fold_value * 2 - y}, else: {x, y}
end)
end
|> Enum.uniq()
|> Enum.count()
Letters
letters = %{
[
{0, 1, 1, 0},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 0, 1}
] => "A",
[
{1, 1, 1, 1},
{1, 0, 0, 0},
{1, 1, 1, 0},
{1, 0, 0, 0},
{1, 0, 0, 0},
{1, 1, 1, 1}
] => "E",
[
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 1},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 0, 0, 1}
] => "H",
[
{1, 1, 1, 0},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 0},
{1, 0, 0, 0},
{1, 0, 0, 0}
] => "P",
[
{1, 1, 1, 0},
{1, 0, 0, 1},
{1, 0, 0, 1},
{1, 1, 1, 0},
{1, 0, 1, 0},
{1, 0, 0, 1}
] => "R",
[
{1, 1, 1, 1},
{0, 0, 0, 1},
{0, 0, 1, 0},
{0, 1, 0, 0},
{1, 0, 0, 0},
{1, 1, 1, 1}
] => "Z"
}
:ok
Part 2 first try
folded_points =
Enum.reduce(folds, points, fn {fold_axis, fold_value}, acc ->
if fold_axis == "x" do
Enum.map(acc, fn {x, y} ->
if x > fold_value, do: {fold_value * 2 - x, y}, else: {x, y}
end)
else
Enum.map(acc, fn {x, y} ->
if y > fold_value, do: {x, fold_value * 2 - y}, else: {x, y}
end)
end
|> Enum.uniq()
end)
[x_max, y_max] = for xy <- 0..1, do: folded_points |> Enum.map(&elem(&1, xy)) |> Enum.max()
blank_paper =
for x <- 0..x_max,
y <- 0..y_max,
into: %{} do
{{x, y}, 0}
end
dotted_paper =
Enum.reduce(folded_points, blank_paper, fn point, acc -> Map.put(acc, point, 1) end)
dotted_paper
|> Map.to_list()
|> Enum.sort()
|> Enum.map(fn {_, val} -> val end)
|> Enum.chunk_every(y_max + 1)
|> Enum.chunk_every(5)
|> Enum.map(
&(&1
|> Enum.slice(0..3)
|> Enum.zip()
|> then(fn grid -> Map.get(letters, grid) end))
)
|> Enum.join()