day 13a complete

This commit is contained in:
Luke Else 2021-12-14 21:20:27 +00:00
parent 5031559529
commit 7a47ce953c
2 changed files with 1018 additions and 18 deletions

View File

@ -5,19 +5,93 @@ import (
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
"strings"
) )
func main() { func main() {
content := returnContent("../input") content := returnContent("../input")
//content := returnContent("testInput") //content := returnContent("testInput")
sheet := make(map[Coordinate]bool)
answer := 0
for _, line := range *content {
if strings.HasPrefix(line, "fold") {
//Fold instructions
instruction := strings.Split(line, "=")
if strings.Contains(instruction[0], "x") {
foldPoint, _ := strconv.Atoi(instruction[1])
sheet = FoldX(sheet, foldPoint)
} }
func returnContent(path string) *[][]int { if strings.Contains(instruction[0], "y") {
foldPoint, _ := strconv.Atoi(instruction[1])
sheet = FoldY(sheet, foldPoint)
}
//Only want to consider the first instruction
//Break means that we don't end up processing the further folds
break
} else if line != "" {
//mapping instructions
coordinates := strings.Split(line, ",")
x, _ := strconv.Atoi(coordinates[0])
y, _ := strconv.Atoi(coordinates[1])
sheet[Coordinate{X: x, Y: y}] = true
}
}
for range sheet {
answer++
}
fmt.Println(answer)
}
func FoldX(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool) {
folded = make(map[Coordinate]bool)
for mark := range sheet {
x := mark.X
if x > foldPoint {
//If the value is in the region that gets folded
x = 2*foldPoint - x
}
folded[Coordinate{X: x, Y: mark.Y}] = true
}
return
}
func FoldY(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool) {
folded = make(map[Coordinate]bool)
for mark := range sheet {
y := mark.X
if y > foldPoint {
//If the value is in the region that gets folded
y = 2*foldPoint - y
}
folded[Coordinate{X: mark.X, Y: y}] = true
}
return
}
type Coordinate struct {
X int
Y int
}
func returnContent(path string) *[]string {
//read file and return it as an array of integers //read file and return it as an array of integers
file, err := os.Open(path) file, err := os.Open(path)
var content [][]int var content []string
if err != nil { if err != nil {
fmt.Println("Unlucky, the file didn't open") fmt.Println("Unlucky, the file didn't open")
@ -28,12 +102,7 @@ func returnContent(path string) *[][]int {
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
nums := []int{} content = append(content, scanner.Text())
for _, char := range scanner.Text() {
num, _ := strconv.Atoi(string(char))
nums = append(nums, num)
}
content = append(content, nums)
} }
return &content return &content

View File

@ -1,10 +1,941 @@
5421451741 92,632
3877321568 795,9
7583273864 1034,368
3451717778 1230,233
2651615156 969,816
6377167526 1218,526
5182852831 971,780
4766856676 139,417
3437187583 892,313
3633371586 1196,196
1091,70
728,747
632,10
1089,247
70,159
492,375
919,581
304,469
736,431
7,630
457,879
174,567
328,773
207,714
361,452
77,151
907,298
325,838
956,644
272,52
259,794
254,847
517,851
669,516
746,275
1146,801
1019,297
679,43
517,416
703,814
90,201
206,74
239,794
1120,114
197,711
1247,131
239,71
418,805
129,757
1195,56
895,598
975,712
57,171
656,746
984,177
1,182
785,457
810,107
703,278
699,593
795,477
67,750
190,616
1,712
162,438
1079,432
1010,868
431,30
293,9
957,518
452,634
1245,452
631,87
395,200
721,284
1143,198
669,378
1205,827
974,483
33,588
894,7
1064,245
1099,616
1307,248
63,234
249,71
919,182
589,284
730,152
251,298
644,78
731,148
669,638
663,712
356,824
960,873
1139,602
902,404
785,728
830,434
691,207
118,567
927,742
436,556
946,866
1235,746
433,647
1266,103
267,775
796,581
474,219
686,582
982,354
897,263
398,626
689,687
1184,294
403,374
303,135
1168,670
1275,562
1000,812
974,411
785,309
1019,108
189,551
969,857
401,549
574,873
73,885
417,406
1029,584
345,626
238,887
103,847
1006,21
919,712
256,268
13,773
1093,644
1230,890
858,260
1010,196
1002,341
761,807
1034,526
661,318
381,390
1111,597
1161,65
1258,152
1031,56
393,890
683,752
22,621
671,49
972,31
581,574
1163,145
1051,694
805,513
1119,28
1051,794
1288,884
1092,226
152,270
174,327
266,151
763,863
733,151
1292,105
55,47
663,630
190,780
1170,537
177,381
550,390
251,150
23,43
1007,135
386,726
431,847
1079,24
171,574
997,212
1056,89
1238,257
74,775
152,119
344,107
833,793
1146,737
972,150
107,600
505,605
1124,273
474,194
17,380
492,823
1309,712
719,131
830,460
1237,574
1180,28
808,176
507,98
339,114
155,261
318,859
335,824
919,548
920,497
1074,413
1221,233
118,103
1079,14
673,518
761,197
289,96
602,294
1064,218
549,197
1091,600
375,80
691,625
965,268
574,469
890,191
796,89
547,415
3,248
858,95
162,71
748,526
222,392
994,526
803,796
868,565
218,42
293,718
763,31
1222,742
279,56
966,107
74,791
455,177
507,796
1071,71
875,518
499,875
177,45
1258,70
659,248
467,750
1044,151
244,714
1260,480
576,476
212,26
181,156
1071,346
239,598
893,406
666,658
1086,401
114,565
171,633
103,495
290,476
1098,420
1268,148
582,257
408,452
310,787
435,376
991,824
1130,518
333,514
1148,413
90,649
649,457
326,298
1155,875
607,814
868,329
1302,859
1048,532
259,884
1006,469
1155,712
587,122
868,250
1155,261
328,705
604,414
271,175
974,247
621,687
1054,211
298,70
647,793
857,432
293,207
190,838
842,173
238,439
113,383
938,530
11,114
1074,295
736,661
1193,666
364,642
525,728
405,590
1089,199
199,877
1206,726
835,301
621,400
1111,297
1238,637
580,551
969,37
945,768
189,343
164,737
890,703
661,514
820,317
13,170
219,175
551,847
512,858
1031,814
1031,260
637,376
1130,667
105,255
1097,735
1305,828
619,625
1126,367
1087,30
13,121
47,726
416,679
338,31
647,630
313,343
729,320
82,571
1139,574
465,491
1017,207
234,847
137,478
875,527
956,26
591,660
514,581
326,189
1303,152
483,857
869,831
483,37
739,863
383,600
502,723
919,96
169,437
33,157
7,712
1250,665
1098,644
792,280
236,413
813,437
107,863
1001,474
1001,626
1092,194
666,638
202,95
223,210
907,596
380,600
1087,847
475,301
114,196
977,206
666,82
119,266
716,823
689,437
303,9
10,658
371,637
773,241
1000,586
935,56
1202,220
699,628
518,280
1076,210
273,838
259,880
972,66
798,450
236,581
763,600
816,227
1250,229
2,250
1120,126
1002,789
731,772
222,56
1057,345
268,726
802,425
335,182
364,252
949,885
464,780
344,630
1130,443
1049,162
206,820
1168,858
982,298
10,275
363,786
1161,605
42,298
1158,492
782,742
171,709
350,682
1240,159
985,504
734,418
364,866
341,485
1203,751
902,732
80,213
338,150
55,847
726,235
536,775
647,661
621,588
445,666
1235,74
954,824
610,791
1000,406
949,562
82,91
1133,45
436,42
1285,638
631,291
416,663
1017,114
874,194
736,233
441,831
902,452
480,434
949,780
272,500
666,12
1099,479
57,311
130,866
631,43
925,499
868,278
42,148
1266,551
657,630
300,226
118,327
1089,647
47,504
1184,742
966,140
556,700
1088,392
239,548
146,376
895,151
549,697
897,631
644,414
657,182
641,638
146,667
652,644
811,709
88,152
1300,598
336,411
1054,716
105,827
1309,630
304,21
1287,851
736,540
883,182
734,194
201,462
1228,120
783,381
1230,213
574,233
1017,885
528,742
905,304
774,551
343,621
836,219
420,703
748,859
500,18
647,600
415,548
300,26
1196,747
152,402
734,866
965,514
364,194
154,787
7,518
25,638
802,441
972,828
171,320
383,495
457,687
355,812
17,868
212,868
1181,885
190,56
793,739
263,182
181,738
1303,712
505,513
565,406
1205,67
508,425
982,637
75,74
795,190
1236,152
1164,376
915,78
293,687
36,630
80,354
1019,597
537,534
1202,642
310,308
92,526
437,644
836,194
155,409
413,631
1193,86
7,264
1017,306
1120,768
1011,304
966,586
728,537
527,65
1193,205
1173,2
1146,295
63,682
1287,535
1253,311
551,47
652,698
443,479
63,548
584,683
256,211
798,892
1268,298
691,114
1220,201
1091,724
1201,868
246,666
1148,800
78,642
1043,775
1161,180
1091,518
1027,628
649,128
1069,183
164,157
417,488
679,87
223,864
1300,236
1130,70
492,325
267,152
935,814
766,182
80,890
219,70
436,338
949,452
939,705
664,859
180,21
517,43
947,513
1146,463
764,252
811,427
1133,849
303,134
736,354
82,452
1007,759
164,352
1017,9
611,628
1238,17
44,103
892,581
1143,646
152,624
846,838
1139,320
890,236
351,424
845,674
1310,11
551,399
1228,263
326,717
1308,698
1072,439
679,291
754,700
331,198
731,820
982,596
80,233
1136,684
1277,306
13,704
1280,582
103,460
647,101
1124,425
319,824
733,375
654,33
1283,306
783,65
201,432
574,4
1038,394
1310,861
938,364
95,656
997,551
200,376
313,78
564,201
902,325
164,381
1201,646
678,10
231,813
361,9
977,514
117,86
646,859
427,182
984,637
52,70
560,142
1101,666
574,764
1143,696
246,228
823,646
127,269
164,463
773,808
408,442
1230,354
151,444
1148,438
146,443
239,738
256,716
656,861
290,264
303,457
249,823
353,518
540,196
338,66
1288,234
482,413
331,696
759,460
197,191
114,147
780,410
811,633
1007,155
313,485
1120,56
1087,658
129,137
345,268
846,278
300,698
508,3
847,28
822,667
487,646
244,442
574,540
689,530
211,616
78,252
159,646
894,663
1098,868
730,551
436,700
1031,437
946,488
11,780
366,858
452,351
73,9
1192,103
1021,798
508,586
1235,148
1232,642
5,437
547,143
663,569
1299,114
10,816
1300,812
684,824
745,406
503,674
1247,96
744,742
115,838
621,207
1183,65
1299,668
474,667
1292,519
361,754
545,248
974,859
579,820
117,205
512,892
509,518
808,12
8,859
1274,264
145,152
836,70
836,451
761,639
1104,820
610,103
811,37
82,442
793,851
241,183
180,451
1062,700
212,278
592,852
1087,864
418,357
743,828
527,829
199,597
483,602
490,705
743,598
80,793
517,739
611,593
1156,787
765,644
864,432
1146,431
763,415
581,885
654,11
1148,879
1044,677
499,467
746,299
180,443
1133,101
33,400
381,196
774,775
847,476
162,456
145,44
171,516
442,250
995,114
1058,600
957,70
43,266
792,320
1297,773
721,452
246,676
649,318
180,667
582,357
892,357
160,219
304,660
746,270
408,121
719,630
579,263
60,677
720,329
431,47
344,219
231,880
299,530
579,122
293,158
1287,43
564,59
845,491
820,766
433,247
1059,744
644,236
949,392
335,712
802,675
734,252
442,565
770,565
171,602
1048,362
502,775
1303,264
1051,365
1293,26
853,879
333,688
644,816
136,574
719,234
392,362
774,119
1087,684
699,266
736,764
1133,381
371,257
793,43
1191,266
341,346
607,373
147,145
905,590
102,849
505,381
1113,703
748,816
782,152
197,183
431,684
139,29
746,624
1247,630
1300,12
1155,19
631,359
72,877
1083,467
382,170
298,824
730,600
946,194
162,15
512,450
1011,28
423,141
488,667
1236,775
793,70
2,644
919,122
856,518
728,257
723,325
833,773
211,415
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