This commit is contained in:
Karl Spickermann 2021-12-05 16:12:59 +01:00
parent 92a5065465
commit 77fcd2c1f8
4 changed files with 633 additions and 0 deletions

106
src/day5/day5.go Normal file
View File

@ -0,0 +1,106 @@
package main
import (
"AOC2021/src/helper"
"fmt"
"os"
"strings"
)
type point struct {
x int
y int
}
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
lines, maxY, maxX := parseInput(input)
calculateSolution(maxY, maxX, lines,false)
calculateSolution(maxY, maxX, lines,true)
}
func calculateSolution(maxY int, maxX int, lines [][2]point, part2 bool) {
field := make([][]int, maxY+1)
for i, _ := range field {
field[i] = make([]int, maxX+1)
}
for _, line := range lines {
drawLine(&field, line, part2)
}
fmt.Println(findOverlaps(field))
}
func parseInput(input []string) ([][2]point, int, int) {
lines := make([][2]point, len(input))
maxY := 0
maxX := 0
for i, row := range input {
points := strings.Split(row, " -> ")
for j, p := range points {
coordinatesStrings := strings.Split(p, ",")
coordinatesInt, _ := helper.MapToNumber(coordinatesStrings)
lines[i][j] = point{coordinatesInt[0], coordinatesInt[1]}
if coordinatesInt[0] > maxX {
maxX = coordinatesInt[0]
}
if coordinatesInt[1] > maxY {
maxY = coordinatesInt[1]
}
}
}
return lines, maxY, maxX
}
func drawLine(field* [][]int, line [2]point, part2 bool){
slope := getSlope(line)
if !part2 && slope.y != 0 && slope.x != 0 {
return
}
i := line[0].x
j := line[0].y
for {
(*field)[j][i] += 1
if i == line[1].x && j == line[1].y {
break
}
i += slope.x
j += slope.y
}
}
func getSlope(line [2]point) point {
x := line[1].x - line[0].x
y := line[1].y - line[0].y
if x == 0 {
return point{x,y / helper.Abs(y)}
}
if y == 0 {
return point{x / helper.Abs(x), y}
}
gcd := helper.GCD(helper.Abs(x), helper.Abs(y))
return point{x / gcd,y / gcd}
}
func printField(field [][]int) {
for _,row := range field {
fmt.Println(row)
}
}
func findOverlaps(field [][]int) int{
sum := 0
for _, row := range field {
for _, p := range row {
if p > 1 {
sum += 1
}
}
}
return sum
}

500
src/day5/day5Input.txt Normal file
View File

@ -0,0 +1,500 @@
529,822 -> 529,562
106,230 -> 568,230
709,377 -> 708,376
600,544 -> 600,638
75,351 -> 70,351
147,959 -> 89,959
626,911 -> 626,805
542,816 -> 408,950
489,224 -> 489,441
266,583 -> 255,583
603,145 -> 603,309
50,871 -> 785,136
460,412 -> 966,918
506,920 -> 506,552
378,515 -> 378,538
16,467 -> 26,467
646,219 -> 646,142
341,461 -> 891,461
382,253 -> 382,945
489,344 -> 634,199
563,19 -> 233,349
337,894 -> 936,295
156,413 -> 156,916
859,986 -> 99,226
226,768 -> 178,816
924,58 -> 924,890
755,236 -> 36,236
920,399 -> 920,643
620,12 -> 790,12
290,236 -> 849,236
556,649 -> 556,96
975,195 -> 591,579
606,438 -> 606,778
871,203 -> 523,551
232,521 -> 904,521
309,206 -> 896,793
385,988 -> 385,186
726,974 -> 95,343
603,336 -> 603,368
749,184 -> 869,184
415,558 -> 911,558
477,550 -> 477,603
522,415 -> 938,831
230,951 -> 497,951
859,275 -> 150,275
683,455 -> 894,455
404,42 -> 658,42
933,767 -> 765,767
909,298 -> 909,423
813,312 -> 833,292
20,15 -> 964,959
376,362 -> 111,627
329,336 -> 329,511
132,915 -> 798,249
517,560 -> 517,479
88,595 -> 88,857
281,678 -> 566,393
144,961 -> 801,304
82,871 -> 545,408
499,849 -> 499,146
414,785 -> 384,785
653,839 -> 653,67
573,634 -> 229,634
880,283 -> 542,283
777,189 -> 164,189
463,97 -> 596,97
862,865 -> 862,555
227,780 -> 227,905
319,214 -> 319,360
683,569 -> 683,335
626,703 -> 88,165
243,810 -> 243,370
622,506 -> 622,324
670,151 -> 670,441
703,808 -> 703,879
260,597 -> 206,597
232,13 -> 94,13
443,680 -> 298,680
504,368 -> 504,103
372,341 -> 372,256
155,799 -> 155,504
12,47 -> 950,985
60,500 -> 60,973
154,783 -> 295,924
131,348 -> 131,497
951,388 -> 542,388
825,164 -> 825,719
850,351 -> 692,351
69,955 -> 259,955
171,701 -> 164,701
748,457 -> 610,319
924,170 -> 924,965
800,599 -> 685,599
621,40 -> 146,515
453,423 -> 800,76
933,394 -> 301,394
207,647 -> 207,255
151,568 -> 662,57
333,763 -> 333,897
521,847 -> 154,847
255,696 -> 97,696
188,687 -> 188,791
76,382 -> 596,902
325,637 -> 618,344
458,949 -> 889,518
365,136 -> 411,136
641,248 -> 641,143
202,518 -> 490,806
225,40 -> 225,493
473,311 -> 968,806
169,854 -> 169,61
17,887 -> 803,101
310,766 -> 508,766
948,12 -> 52,908
953,739 -> 347,739
183,727 -> 757,153
101,21 -> 101,799
461,619 -> 461,831
519,390 -> 857,390
615,860 -> 72,860
846,87 -> 846,897
826,896 -> 560,896
237,206 -> 982,951
407,825 -> 949,283
555,705 -> 555,333
180,101 -> 180,452
144,31 -> 144,192
894,175 -> 894,927
468,57 -> 146,379
117,66 -> 117,530
187,748 -> 187,948
150,232 -> 685,767
630,249 -> 630,400
591,679 -> 762,508
888,177 -> 404,177
524,692 -> 944,272
677,104 -> 818,104
54,232 -> 573,232
618,629 -> 618,456
62,128 -> 938,128
895,113 -> 111,897
192,165 -> 969,942
890,896 -> 890,329
714,643 -> 84,13
561,268 -> 561,818
700,534 -> 493,741
965,582 -> 652,269
840,526 -> 285,526
216,86 -> 216,886
812,275 -> 708,275
113,902 -> 912,902
137,173 -> 137,553
343,598 -> 193,598
510,380 -> 194,380
37,974 -> 957,54
119,801 -> 888,801
243,11 -> 813,581
298,463 -> 298,884
86,869 -> 86,727
431,899 -> 581,899
788,862 -> 633,862
305,781 -> 196,781
478,421 -> 426,421
956,47 -> 32,971
689,802 -> 689,525
940,94 -> 940,455
896,207 -> 120,207
569,360 -> 377,168
673,281 -> 673,396
791,839 -> 50,98
66,787 -> 837,16
13,887 -> 809,91
550,745 -> 550,494
970,901 -> 320,251
832,49 -> 61,820
265,577 -> 795,47
556,211 -> 106,211
809,216 -> 50,216
81,630 -> 292,630
69,973 -> 986,56
389,240 -> 389,98
526,748 -> 526,573
975,885 -> 567,885
665,492 -> 665,407
169,726 -> 926,726
609,896 -> 609,560
599,398 -> 403,202
436,367 -> 436,907
859,268 -> 308,268
369,331 -> 525,331
116,19 -> 116,691
457,845 -> 452,840
43,117 -> 828,902
839,938 -> 839,282
344,154 -> 975,154
492,952 -> 186,952
450,94 -> 450,650
823,897 -> 743,897
787,112 -> 787,75
196,921 -> 233,958
969,976 -> 73,80
593,873 -> 354,634
51,838 -> 630,838
523,822 -> 718,822
990,235 -> 807,235
355,789 -> 15,789
465,919 -> 609,919
662,185 -> 455,185
722,406 -> 722,346
20,283 -> 20,270
608,440 -> 283,440
215,756 -> 215,956
324,275 -> 866,817
319,181 -> 319,445
397,272 -> 50,619
281,354 -> 266,354
915,858 -> 915,64
815,723 -> 340,248
873,760 -> 873,532
677,145 -> 458,364
409,106 -> 54,106
867,585 -> 704,585
100,617 -> 100,34
708,707 -> 418,707
535,812 -> 535,841
294,571 -> 294,851
824,590 -> 234,590
534,317 -> 938,317
13,133 -> 13,245
784,74 -> 130,728
907,916 -> 668,677
526,767 -> 849,767
930,96 -> 824,96
399,33 -> 399,348
910,404 -> 910,690
455,348 -> 455,737
741,464 -> 685,464
169,296 -> 952,296
947,388 -> 491,844
964,336 -> 964,230
614,71 -> 240,71
877,957 -> 877,896
37,381 -> 37,871
184,772 -> 149,772
928,120 -> 928,772
926,434 -> 394,966
984,130 -> 138,976
906,346 -> 376,876
697,220 -> 697,166
431,301 -> 130,301
419,549 -> 419,227
714,14 -> 714,303
257,644 -> 127,644
186,950 -> 975,161
95,671 -> 601,671
189,109 -> 314,109
858,701 -> 627,701
47,155 -> 467,575
334,767 -> 334,395
609,482 -> 609,647
980,334 -> 497,334
162,723 -> 399,723
962,961 -> 45,44
473,296 -> 452,296
155,49 -> 155,612
379,497 -> 552,324
640,606 -> 414,606
889,604 -> 889,616
564,273 -> 47,790
181,770 -> 656,770
451,281 -> 969,281
124,289 -> 59,289
359,106 -> 359,730
535,644 -> 33,142
486,651 -> 399,651
722,453 -> 944,453
920,46 -> 223,46
369,954 -> 369,387
455,430 -> 455,752
57,22 -> 973,938
337,887 -> 337,313
546,586 -> 471,586
762,908 -> 762,194
885,259 -> 402,742
889,348 -> 889,111
885,963 -> 741,819
340,72 -> 340,822
139,683 -> 664,158
814,28 -> 814,908
115,623 -> 137,623
546,69 -> 211,69
151,620 -> 151,183
656,674 -> 464,482
206,445 -> 187,464
105,375 -> 105,216
352,952 -> 352,832
964,840 -> 826,978
392,201 -> 392,428
400,536 -> 400,798
283,62 -> 71,62
70,818 -> 893,818
111,252 -> 721,252
952,101 -> 124,929
75,248 -> 75,52
299,83 -> 962,746
680,344 -> 680,524
13,987 -> 989,11
844,809 -> 755,898
402,294 -> 977,869
302,403 -> 692,793
86,11 -> 202,11
413,766 -> 124,766
689,146 -> 967,146
311,88 -> 717,88
574,810 -> 432,668
666,330 -> 914,330
22,988 -> 986,24
849,921 -> 849,563
434,441 -> 136,143
185,896 -> 469,612
53,553 -> 53,916
554,51 -> 554,956
28,116 -> 857,945
756,933 -> 759,936
985,953 -> 963,953
339,396 -> 339,194
813,763 -> 813,124
321,561 -> 795,87
809,759 -> 503,759
291,94 -> 300,94
624,652 -> 601,675
115,461 -> 560,906
194,653 -> 194,148
728,695 -> 728,409
95,472 -> 95,885
806,503 -> 223,503
260,610 -> 260,874
73,130 -> 772,829
963,15 -> 11,967
661,409 -> 978,92
293,982 -> 293,249
839,17 -> 839,668
138,518 -> 138,471
749,348 -> 749,177
624,176 -> 835,387
943,88 -> 57,974
88,610 -> 552,610
939,500 -> 909,530
629,48 -> 440,48
903,612 -> 96,612
379,914 -> 209,914
51,22 -> 51,263
985,975 -> 66,56
39,120 -> 699,780
465,93 -> 967,93
765,954 -> 765,918
405,315 -> 710,315
819,881 -> 45,107
531,111 -> 355,287
883,443 -> 659,443
288,46 -> 763,521
768,615 -> 768,662
357,53 -> 848,544
351,782 -> 351,317
641,944 -> 938,647
360,905 -> 982,283
359,117 -> 359,554
937,467 -> 937,281
716,963 -> 55,963
257,299 -> 550,299
28,14 -> 952,938
326,988 -> 326,65
787,227 -> 501,513
356,162 -> 736,162
668,562 -> 668,292
463,850 -> 36,850
979,105 -> 979,662
314,24 -> 682,24
898,406 -> 780,406
199,43 -> 572,43
730,120 -> 156,120
613,573 -> 217,573
541,489 -> 948,896
359,921 -> 845,921
174,365 -> 616,365
962,179 -> 962,42
516,92 -> 101,92
695,233 -> 61,233
17,24 -> 978,985
223,409 -> 223,719
489,815 -> 637,815
346,249 -> 955,858
101,687 -> 461,687
243,427 -> 334,518
849,915 -> 203,269
11,758 -> 535,758
76,788 -> 804,60
659,343 -> 649,343
756,602 -> 756,548
543,593 -> 543,788
36,65 -> 950,979
522,96 -> 157,96
839,600 -> 797,600
571,704 -> 210,704
704,361 -> 704,409
374,799 -> 974,199
870,826 -> 64,20
30,618 -> 30,196
394,260 -> 394,934
793,65 -> 815,65
126,769 -> 756,769
443,636 -> 443,749
909,573 -> 831,495
22,884 -> 891,15
337,286 -> 337,454
261,607 -> 210,556
314,837 -> 314,774
869,432 -> 869,957
482,81 -> 475,74
966,94 -> 103,957
759,102 -> 655,206
956,95 -> 956,714
765,681 -> 765,520
817,959 -> 201,959
301,378 -> 725,802
56,867 -> 421,867
775,657 -> 430,657
340,57 -> 565,57
615,632 -> 958,289
265,342 -> 138,469
804,654 -> 810,654
782,306 -> 782,483
399,683 -> 982,683
593,781 -> 796,781
638,752 -> 638,323
122,243 -> 122,843
890,83 -> 54,919
376,552 -> 376,380
271,403 -> 302,403
684,221 -> 16,889
286,618 -> 424,480
738,693 -> 138,93
629,750 -> 629,284
734,364 -> 734,738
916,934 -> 19,37
801,342 -> 954,495
237,516 -> 606,885
680,591 -> 65,591
661,230 -> 756,230
163,371 -> 163,496
374,759 -> 859,759
945,513 -> 705,753
239,517 -> 239,916
548,796 -> 72,320
441,351 -> 441,402
129,315 -> 793,979
892,137 -> 819,137
799,957 -> 799,731
649,468 -> 626,468
974,299 -> 974,629
828,954 -> 88,214
369,277 -> 312,277
943,360 -> 943,314
610,644 -> 610,647
172,913 -> 460,625
145,61 -> 888,804
813,693 -> 582,693
857,848 -> 49,40
660,252 -> 387,252
250,723 -> 25,723
340,186 -> 621,186
144,96 -> 144,372
351,804 -> 366,804
875,600 -> 752,600
475,490 -> 865,880
26,46 -> 944,964
405,712 -> 125,712
711,676 -> 178,143
488,446 -> 348,306
170,440 -> 170,105
970,981 -> 119,130
284,772 -> 416,772
850,148 -> 749,148
898,955 -> 898,947
677,109 -> 787,219
534,650 -> 534,696
730,216 -> 730,814
514,89 -> 130,89
380,199 -> 291,288
931,110 -> 678,363
226,537 -> 453,764
954,228 -> 954,988
847,338 -> 315,338
316,989 -> 974,331
666,554 -> 224,554
53,868 -> 551,370
614,548 -> 614,924
191,837 -> 561,837
605,639 -> 240,274
906,28 -> 906,957

10
src/day5/day5Test.txt Normal file
View File

@ -0,0 +1,10 @@
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2

View File

@ -130,3 +130,20 @@ func Delete (stringArray []string, selector string) []string {
}
return returnString
}
func GCD(x int,y int) (int){
gcd := -1
for i := 1; i <=x && i <=y ; i++ {
if(y%i==0 && x%i==0) {
gcd = i
}
}
return gcd
}
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}