jlelse
/
aoc21
Archived
1
Fork 0
This commit is contained in:
Jan-Lukas Else 2021-12-13 14:58:29 +01:00
parent 333a4c9675
commit 72b053def2
4 changed files with 1049 additions and 0 deletions

890
13/input.txt Normal file
View File

@ -0,0 +1,890 @@
1159,502
291,184
850,631
460,631
301,707
768,56
654,829
582,466
490,276
261,3
793,35
559,453
497,288
594,275
458,264
1044,387
654,842
925,22
688,72
224,833
1228,2
1034,487
420,830
172,717
1049,198
669,305
1168,49
157,304
90,669
1036,1
118,549
1084,593
268,61
909,294
467,74
1114,632
1066,609
1058,602
271,35
474,299
1173,35
75,864
470,543
176,100
485,736
199,332
23,634
654,329
748,526
274,395
378,474
910,673
1012,810
383,859
1163,5
353,291
741,383
631,241
179,436
793,75
495,324
321,169
1087,278
1302,361
1232,572
417,317
960,161
1088,642
1161,613
631,653
94,327
244,733
857,549
572,220
75,337
259,5
1235,192
805,393
126,68
1250,723
580,68
397,441
1297,165
700,82
1200,275
214,56
932,711
676,126
676,320
169,388
1049,3
25,319
1218,215
179,217
681,635
512,574
966,371
1124,282
387,444
930,644
179,781
192,247
1061,290
500,557
378,54
855,159
585,891
358,295
545,234
1287,429
425,648
416,579
1255,99
59,290
768,585
1126,733
253,600
1235,332
852,184
223,357
1143,585
1096,36
1125,194
95,677
885,768
870,820
165,527
1201,660
465,291
387,702
1041,872
502,316
358,814
435,747
1217,439
177,113
505,781
850,746
343,38
499,530
85,789
1285,563
321,541
45,15
1295,669
1285,393
363,262
271,347
646,809
1143,794
505,753
224,201
50,420
2,297
507,312
823,100
222,252
1007,241
1116,157
274,499
505,617
1036,137
987,623
22,105
30,239
244,609
835,333
1019,184
572,443
134,613
348,263
927,35
500,625
421,807
763,595
277,838
856,54
1310,52
855,287
166,628
1155,684
609,327
445,892
1116,640
159,346
1116,478
1116,849
982,565
1232,754
1163,337
0,136
422,455
1049,451
857,345
59,640
688,822
507,582
351,485
294,744
1218,383
751,665
969,761
50,165
1253,358
393,59
629,331
550,842
30,80
505,113
731,30
641,761
513,780
221,543
1131,561
1072,315
252,602
87,556
1148,282
902,61
276,308
35,250
1057,600
403,785
113,502
805,501
0,86
1028,364
428,301
1305,353
616,645
199,607
744,68
350,385
412,530
574,554
713,806
1193,327
923,514
952,743
542,586
890,830
378,26
0,394
214,523
335,210
75,30
845,282
395,357
1285,187
597,806
160,154
589,760
52,843
654,117
711,170
137,78
1130,595
959,37
1220,690
797,252
923,444
1285,259
55,99
980,638
641,305
781,549
1126,768
184,161
915,477
731,702
888,868
537,194
738,219
1288,777
982,285
1087,357
595,254
1288,161
266,21
1310,136
1144,745
1111,562
671,893
1198,756
1176,389
1123,372
748,831
1168,400
810,886
805,141
705,614
269,760
417,126
1044,313
1228,226
1158,204
288,645
870,504
266,313
1253,470
954,84
134,806
420,694
348,711
957,623
1285,271
1007,485
428,593
494,493
1051,5
274,698
852,150
1216,327
559,477
522,625
691,644
261,787
823,346
773,775
907,254
1288,789
728,466
559,665
664,809
833,656
258,679
422,474
877,95
1253,246
822,709
224,542
154,456
502,302
171,231
1253,872
1260,824
256,176
372,364
358,743
1056,225
1049,787
1168,285
679,465
157,590
214,820
1051,89
652,358
192,80
888,474
679,241
430,201
681,53
572,668
626,501
724,663
78,98
639,229
591,495
798,721
494,857
437,10
1131,312
1028,194
72,428
1014,151
229,117
1086,835
923,892
515,485
485,288
366,249
1088,252
745,390
266,425
880,649
952,814
656,329
865,78
440,504
397,889
706,294
1096,56
177,672
900,285
154,785
803,582
1126,126
174,466
438,171
658,760
57,694
962,183
440,820
57,134
1138,774
430,733
107,756
1149,600
1242,889
997,57
1156,886
331,497
947,856
671,445
658,358
1118,169
1156,456
276,488
873,884
267,10
880,61
805,589
944,249
412,812
326,394
1265,816
460,74
562,526
377,56
552,61
291,698
822,194
959,241
656,758
1071,332
22,341
565,504
1052,679
1091,299
1217,639
852,296
1268,394
738,883
1238,225
938,194
811,124
1039,347
507,648
460,737
1019,849
177,841
798,270
319,684
915,865
117,226
1006,509
274,163
561,535
497,606
585,3
185,52
30,814
348,183
221,799
395,477
749,887
957,603
1014,631
1310,842
723,473
214,717
1290,768
50,40
1087,2
797,556
425,768
1163,785
387,514
1302,809
813,606
649,786
189,719
343,843
947,262
992,439
93,639
261,451
13,133
932,837
1153,590
582,109
1053,565
922,581
634,320
311,856
1176,281
1265,368
460,605
1084,301
1144,73
341,837
706,729
303,485
445,450
960,385
353,603
1151,122
160,37
564,690
798,574
1049,891
885,563
363,710
879,537
840,49
751,229
1285,779
868,562
353,623
1059,109
269,134
257,329
788,711
462,590
74,490
706,600
602,753
229,246
602,312
1144,628
646,231
1288,105
566,826
619,625
440,717
536,549
1275,250
477,501
1168,721
907,108
708,507
1039,547
13,729
93,455
1118,647
1161,165
74,378
417,518
142,721
967,843
1307,436
947,710
699,773
1280,814
425,200
1260,138
110,395
356,84
1310,381
1146,86
522,631
110,499
5,353
629,563
1242,679
840,319
276,756
134,358
1287,689
152,690
1260,698
217,682
1034,57
440,522
201,773
1193,567
870,729
1253,424
301,719
8,533
738,443
706,165
475,458
256,718
298,875
999,856
894,740
105,246
420,679
668,810
1073,765
301,603
853,756
763,138
991,796
954,19
400,428
1144,266
323,497
938,364
708,312
462,395
870,74
52,644
982,665
1066,40
433,95
421,535
1230,788
997,110
1019,698
161,600
1094,420
559,417
923,702
1022,645
166,821
505,841
162,282
773,119
90,690
977,786
586,231
223,892
457,756
1232,322
512,721
137,443
1201,234
979,49
1258,283
97,246
422,868
1009,187
889,87
251,416
1202,492
1302,533
763,756
597,424
311,221
313,589
1253,134
810,557
1235,30
356,19
982,829
1145,527
132,30
885,648
654,381
654,65
234,816
458,710
1111,332
1019,710
331,49
561,7
440,281
360,150
261,198
440,165
412,59
1131,582
78,754
1009,164
587,501
1029,773
746,690
50,600
1260,756
117,327
257,441
582,557
89,481
400,669
216,249
701,190
50,196
1302,737
933,838
421,87
82,674
80,554
951,30
1089,799
1163,458
562,816
1141,836
65,507
110,691
162,612
189,368
490,170
634,520
179,582
656,198
160,857
1041,760
549,205
994,712
1236,516
1280,239
201,121
706,586
1131,436
629,501
237,70
1285,331
898,82
1230,554
350,161
59,254
945,194
1071,780
1034,308
878,833
1220,669
1049,835
1043,10
142,46
403,109
599,547
403,640
872,723
351,37
562,511
1131,113
462,695
1252,315
179,333
842,282
460,254
887,665
184,768
529,549
269,872
1096,717
117,345
1089,95
159,122
475,436
803,246
597,312
605,614
751,417
542,56
751,453
1015,702
713,760
822,681
595,640
895,108
1056,18
1220,231
1150,717
1173,443
269,470
147,5
214,726
667,0
1243,537
159,621
349,170
1305,338
388,581
611,773
160,149
1228,450
550,700
875,747
305,668
1173,3
418,679
102,833
500,886
1148,612
923,436
894,154
813,288
996,660
922,469
55,302
902,891
1168,718
403,108
490,808
458,184
857,247
678,553
662,887
149,165
1238,785
805,672
520,389
1202,402
1101,700
870,372
1193,119
788,631
734,572
793,371
661,556
1168,848
224,352
505,277
562,368
11,0
888,455
1131,781
865,892
1006,52
298,19
714,385
209,700
803,436
7,679
579,702
522,263
1150,269
1252,586
160,73
1238,428
103,380
1278,374
808,578
169,288
495,845
668,84
137,451
338,182
850,737
587,393
721,555
1275,698
741,105
774,866
803,648
1039,35
1253,200
1303,679
831,278
843,633
421,439
78,572
576,572
852,710
259,889
416,740
294,150
184,817
678,777
994,182
442,562
393,3
885,200
669,761
1150,149
1034,474
542,585
189,67
820,276
65,339
31,416
209,567
502,578
1081,648
445,78
1118,247
217,458
336,455
1126,817
805,753
331,677
25,331
336,439
92,158
960,733
137,603
174,428
734,140
460,157
43,569
58,586
196,632
313,784
199,287
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

121
13/main.go Normal file
View File

@ -0,0 +1,121 @@
package main
import (
"log"
"os"
"strconv"
"strings"
)
func main() {
inputBytes, _ := os.ReadFile("input.txt")
input := strings.Split(string(inputBytes), "\n")
log.Println("Puzzle 1:", puzzle1(input))
log.Println("Puzzle 2:\n" + puzzle2(input))
}
func puzzle1(input []string) int {
grid := fold(input, false)
count := 0
for _, row := range grid {
for _, cell := range row {
if cell == 1 {
count++
}
}
}
return count
}
func puzzle2(input []string) string {
grid := fold(input, true)
res := ""
for _, row := range grid {
for _, cell := range row {
if cell == 1 {
res += "#"
} else {
res += " "
}
}
res += "\n"
}
return res
}
func fold(input []string, puzzle2 bool) [][]int {
// Parse input
xs := []int{}
ys := []int{}
folds := []string{}
for _, line := range input {
if line == "" {
continue
}
if strings.HasPrefix(line, "fold along") {
folds = append(folds, strings.TrimPrefix(line, "fold along "))
continue
}
parts := strings.Split(line, ",")
x, _ := strconv.Atoi(parts[0])
xs = append(xs, x)
y, _ := strconv.Atoi(parts[1])
ys = append(ys, y)
}
// Get maximum x and y
var maxX, maxY int
for _, x := range xs {
if x > maxX {
maxX = x
}
}
for _, y := range ys {
if y > maxY {
maxY = y
}
}
// Create grid
grid := make([][]int, maxY+1)
for i := range grid {
grid[i] = make([]int, maxX+1)
}
// Fill grid
for i, x := range xs {
grid[ys[i]][x] = 1
}
// Fold grid
for fi, f := range folds {
if fi > 0 && !puzzle2 {
break
}
parts := strings.Split(f, "=")
p, _ := strconv.Atoi(parts[1])
xl := len(grid[0])
yl := len(grid)
if parts[0] == "x" {
// Copy all points from right of the fold to the left and shrink the grid
for j := 0; j < yl; j++ {
for i := 0; i+p < xl && p-i >= 0; i++ {
if grid[j][p+i] == 1 {
grid[j][p-i] = 1
}
}
// Shrink row
grid[j] = grid[j][:p]
}
} else {
// Copy all points from bottom of the fold to the top
for j := 0; j+p < yl && p-j >= 0; j++ {
for i := 0; i < xl; i++ {
if grid[p+j][i] == 1 {
grid[p-j][i] = 1
}
}
}
// Shrink grid
grid = grid[:p]
}
}
return grid
}

17
13/main_test.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test(t *testing.T) {
inputBytes, _ := os.ReadFile("test.txt")
input := strings.Split(string(inputBytes), "\n")
assert.Equal(t, 17, puzzle1(input))
// assert.Equal(t, 0, puzzle2(input)) // Can't test puzzle2
}

21
13/test.txt Normal file
View File

@ -0,0 +1,21 @@
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