diff --git a/day5/day 5a/go.mod b/day5/day 5a/go.mod new file mode 100644 index 0000000..e938e2e --- /dev/null +++ b/day5/day 5a/go.mod @@ -0,0 +1,3 @@ +module PWD + +go 1.17 diff --git a/day5/day 5a/main.go b/day5/day 5a/main.go new file mode 100644 index 0000000..eef51c9 --- /dev/null +++ b/day5/day 5a/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "bufio" + "fmt" + "math" + "os" + "regexp" + "strconv" +) + +func main() { + content := returnContent("../input") + //content := returnContent("testInput") + + gameMap := [1000][1000]int{} + + fmt.Println(run(content, &gameMap)) +} + +func run(content *[][]int, gameMap *[1000][1000]int) int { + for i := 0; i < len((*content)); i++ { + fillMap(gameMap, (*content)[i][0], (*content)[i][1], (*content)[i][2], (*content)[i][3]) + } + + count := 0 + + for i := 0; i < len(gameMap); i++ { + for j := 0; j < len(gameMap[i]); j++ { + if gameMap[i][j] > 1 { + count++ + } + } + } + return count +} + +func fillMap(gameMap *[1000][1000]int, x1 int, y1 int, x2 int, y2 int) { + + if x1 == x2 { + for i := int(math.Min(float64(y1), float64(y2))); i <= int(math.Max(float64(y1), float64(y2))); i++ { + (*gameMap)[i][x1]++ + } + } else if y1 == y2 { + //fill horizontal line + for i := int(math.Min(float64(x1), float64(x2))); i <= int(math.Max(float64(x1), float64(x2))); i++ { + (*gameMap)[y1][i]++ + } + } +} + +func returnContent(path string) *[][]int { + //read file and return it as an array of integers + + file, err := os.Open(path) + var content [][]int + + if err != nil { + fmt.Println("Unlucky, the file didn't open") + return &content + } + defer file.Close() + + scanner := bufio.NewScanner(file) + regex := regexp.MustCompile("([0-9]+)") + for scanner.Scan() { + stringValues := regex.FindAllString(scanner.Text(), 4) + intValues := []int{} + for _, v := range stringValues { + num, _ := strconv.Atoi(v) + intValues = append(intValues, num) + } + content = append(content, intValues) + } + + return &content +} diff --git a/day5/day 5a/testInput b/day5/day 5a/testInput new file mode 100644 index 0000000..1d4e36d --- /dev/null +++ b/day5/day 5a/testInput @@ -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 \ No newline at end of file diff --git a/day5/input b/day5/input new file mode 100644 index 0000000..f94e529 --- /dev/null +++ b/day5/input @@ -0,0 +1,500 @@ +102,578 -> 363,317 +536,470 -> 536,863 +578,460 -> 203,835 +42,859 -> 247,859 +618,147 -> 147,618 +119,317 -> 119,22 +14,975 -> 950,39 +245,359 -> 245,877 +835,278 -> 159,954 +663,103 -> 558,103 +194,85 -> 194,193 +77,529 -> 77,208 +677,459 -> 515,459 +867,775 -> 867,482 +674,508 -> 191,508 +926,528 -> 614,528 +816,467 -> 816,765 +963,609 -> 963,537 +838,400 -> 915,400 +53,546 -> 297,546 +745,938 -> 396,589 +820,30 -> 820,114 +351,406 -> 351,212 +356,309 -> 356,533 +592,221 -> 179,634 +87,151 -> 412,151 +350,867 -> 350,616 +383,505 -> 383,537 +954,768 -> 298,112 +437,434 -> 437,92 +11,921 -> 917,15 +942,919 -> 87,64 +236,690 -> 297,690 +290,573 -> 290,823 +582,976 -> 582,521 +515,708 -> 515,289 +644,175 -> 448,175 +495,683 -> 649,683 +697,180 -> 616,180 +410,785 -> 410,368 +451,677 -> 431,677 +186,625 -> 186,202 +372,816 -> 263,707 +787,831 -> 787,802 +813,823 -> 813,299 +69,49 -> 69,592 +280,794 -> 389,794 +853,876 -> 853,525 +160,796 -> 867,89 +489,388 -> 954,388 +408,366 -> 682,366 +753,624 -> 753,407 +322,846 -> 322,296 +10,630 -> 312,328 +250,414 -> 250,188 +375,383 -> 131,627 +110,875 -> 330,655 +292,158 -> 229,158 +983,975 -> 60,52 +908,374 -> 760,226 +325,711 -> 325,483 +857,369 -> 397,369 +618,178 -> 618,504 +460,965 -> 53,558 +536,979 -> 176,979 +953,819 -> 953,252 +578,317 -> 578,326 +406,425 -> 406,219 +785,782 -> 161,158 +593,908 -> 593,973 +979,10 -> 522,10 +421,474 -> 763,132 +924,350 -> 924,869 +982,38 -> 182,838 +621,39 -> 297,39 +328,242 -> 328,601 +89,909 -> 657,341 +546,304 -> 931,304 +963,526 -> 963,564 +57,596 -> 57,89 +436,353 -> 436,731 +824,963 -> 824,513 +594,744 -> 594,233 +769,667 -> 769,625 +707,410 -> 707,561 +244,511 -> 244,712 +362,81 -> 656,375 +302,652 -> 302,814 +957,394 -> 885,466 +522,760 -> 769,760 +13,13 -> 981,981 +154,133 -> 146,125 +355,11 -> 838,494 +31,73 -> 904,946 +450,603 -> 130,603 +610,42 -> 479,173 +67,975 -> 873,169 +927,251 -> 725,49 +617,179 -> 154,642 +618,611 -> 205,198 +960,314 -> 891,383 +349,501 -> 492,358 +853,135 -> 963,245 +503,383 -> 906,383 +164,265 -> 164,647 +616,219 -> 190,645 +117,888 -> 794,211 +864,461 -> 805,461 +461,346 -> 461,553 +639,242 -> 37,242 +256,745 -> 719,745 +285,26 -> 285,87 +596,140 -> 596,204 +722,800 -> 34,800 +819,952 -> 940,952 +214,15 -> 863,664 +94,957 -> 94,254 +259,632 -> 259,842 +808,64 -> 378,64 +592,407 -> 592,560 +640,474 -> 640,520 +425,42 -> 959,576 +443,527 -> 245,725 +347,390 -> 334,390 +188,839 -> 188,190 +186,93 -> 958,865 +700,238 -> 700,265 +687,644 -> 687,946 +264,823 -> 353,823 +944,200 -> 235,909 +970,289 -> 970,57 +51,834 -> 177,708 +460,232 -> 353,232 +552,692 -> 945,299 +900,360 -> 666,360 +829,491 -> 829,459 +666,365 -> 490,365 +232,506 -> 88,506 +522,348 -> 513,339 +349,321 -> 369,341 +974,816 -> 21,816 +187,84 -> 863,760 +400,47 -> 216,47 +342,491 -> 813,20 +461,921 -> 352,921 +440,846 -> 440,275 +192,541 -> 616,965 +214,288 -> 214,78 +185,376 -> 499,62 +849,756 -> 739,866 +844,951 -> 50,157 +195,481 -> 397,279 +215,460 -> 215,180 +889,745 -> 496,352 +696,57 -> 696,297 +622,900 -> 289,900 +468,904 -> 468,347 +867,984 -> 405,984 +276,461 -> 494,461 +111,239 -> 111,746 +832,212 -> 361,212 +152,137 -> 969,954 +735,821 -> 735,639 +962,467 -> 617,812 +361,26 -> 361,35 +389,181 -> 389,669 +845,123 -> 19,949 +11,17 -> 981,987 +532,861 -> 532,408 +215,626 -> 109,626 +363,544 -> 363,585 +879,984 -> 879,153 +798,213 -> 798,254 +805,648 -> 764,689 +617,505 -> 617,296 +822,862 -> 311,862 +845,830 -> 845,631 +958,483 -> 958,290 +323,167 -> 323,793 +429,451 -> 429,737 +337,846 -> 337,245 +637,388 -> 62,963 +971,11 -> 971,154 +11,260 -> 11,661 +81,72 -> 989,980 +267,494 -> 267,605 +872,783 -> 872,425 +286,982 -> 286,873 +925,291 -> 925,881 +868,987 -> 60,179 +664,458 -> 973,458 +225,227 -> 225,909 +302,140 -> 657,140 +796,193 -> 55,934 +527,137 -> 527,777 +134,209 -> 442,517 +123,11 -> 923,811 +626,870 -> 290,534 +312,574 -> 312,142 +87,197 -> 87,13 +350,294 -> 886,294 +363,829 -> 891,301 +802,486 -> 580,708 +440,691 -> 249,691 +382,479 -> 382,399 +865,839 -> 865,28 +983,714 -> 277,714 +221,404 -> 221,739 +894,519 -> 894,727 +593,489 -> 880,489 +941,940 -> 70,69 +458,838 -> 723,838 +653,782 -> 653,669 +304,526 -> 543,526 +576,258 -> 576,396 +988,989 -> 12,13 +878,18 -> 878,236 +232,493 -> 232,376 +62,743 -> 312,743 +115,329 -> 115,839 +940,937 -> 33,30 +255,88 -> 255,316 +866,790 -> 320,790 +948,427 -> 948,327 +773,880 -> 105,212 +645,861 -> 936,861 +196,279 -> 196,648 +213,740 -> 213,167 +581,513 -> 703,513 +787,117 -> 330,574 +222,101 -> 827,101 +175,928 -> 981,928 +283,923 -> 239,923 +21,394 -> 21,880 +255,943 -> 255,968 +56,304 -> 56,200 +537,83 -> 537,120 +536,164 -> 421,164 +610,600 -> 610,168 +33,348 -> 78,348 +510,462 -> 109,863 +213,95 -> 910,792 +700,537 -> 361,537 +830,28 -> 894,92 +624,15 -> 624,442 +166,335 -> 561,335 +635,684 -> 962,357 +917,379 -> 524,772 +693,890 -> 195,890 +354,511 -> 455,511 +340,443 -> 333,450 +936,73 -> 266,73 +271,325 -> 271,125 +134,268 -> 298,268 +789,424 -> 904,309 +32,142 -> 32,903 +13,230 -> 749,966 +710,559 -> 710,679 +45,529 -> 167,529 +783,191 -> 921,191 +587,522 -> 91,26 +541,753 -> 762,532 +570,589 -> 375,589 +543,172 -> 869,172 +370,445 -> 374,445 +234,742 -> 869,742 +902,673 -> 68,673 +166,725 -> 865,26 +802,164 -> 239,727 +953,184 -> 368,769 +652,97 -> 652,298 +414,469 -> 414,344 +720,458 -> 347,85 +769,741 -> 130,741 +105,179 -> 879,953 +116,322 -> 116,502 +428,637 -> 398,637 +596,217 -> 896,217 +134,943 -> 233,943 +230,295 -> 262,295 +856,254 -> 259,254 +863,467 -> 863,944 +561,627 -> 561,258 +805,68 -> 142,731 +27,938 -> 889,76 +888,103 -> 185,806 +202,801 -> 509,494 +833,758 -> 833,554 +792,772 -> 144,124 +123,561 -> 123,362 +908,420 -> 817,420 +324,674 -> 324,629 +542,488 -> 638,488 +188,109 -> 497,109 +880,868 -> 46,868 +438,185 -> 438,848 +583,299 -> 583,538 +233,333 -> 534,333 +422,292 -> 573,292 +376,383 -> 619,383 +436,787 -> 668,787 +982,340 -> 982,857 +36,426 -> 49,426 +118,141 -> 771,794 +814,836 -> 926,836 +982,322 -> 887,322 +962,21 -> 962,405 +520,689 -> 287,922 +275,840 -> 275,424 +28,492 -> 299,763 +717,810 -> 717,717 +184,136 -> 258,136 +546,264 -> 546,800 +402,465 -> 850,913 +138,836 -> 138,110 +517,766 -> 517,358 +937,80 -> 102,915 +877,699 -> 877,930 +317,227 -> 192,352 +439,374 -> 439,332 +761,558 -> 761,628 +378,927 -> 836,469 +133,490 -> 133,968 +728,930 -> 176,930 +353,488 -> 353,173 +457,961 -> 980,961 +234,980 -> 234,111 +251,568 -> 251,846 +411,789 -> 52,789 +152,754 -> 152,21 +796,346 -> 264,878 +492,201 -> 86,201 +215,59 -> 896,740 +630,768 -> 57,768 +863,148 -> 863,947 +62,862 -> 208,862 +846,819 -> 279,252 +495,530 -> 495,515 +211,275 -> 111,275 +613,439 -> 613,878 +70,565 -> 70,84 +674,691 -> 674,164 +126,247 -> 126,888 +551,602 -> 274,602 +147,198 -> 560,198 +824,262 -> 245,841 +63,37 -> 965,939 +942,541 -> 942,439 +909,69 -> 37,941 +141,798 -> 976,798 +854,971 -> 854,689 +395,188 -> 41,188 +556,185 -> 282,459 +400,213 -> 956,213 +820,195 -> 820,186 +301,821 -> 301,628 +529,491 -> 446,408 +826,129 -> 93,862 +648,474 -> 387,474 +168,932 -> 616,932 +392,387 -> 392,819 +736,736 -> 33,33 +514,538 -> 514,255 +703,884 -> 634,884 +692,697 -> 131,697 +826,148 -> 249,148 +282,620 -> 169,507 +878,755 -> 878,820 +985,918 -> 205,138 +79,36 -> 682,639 +897,655 -> 879,655 +136,829 -> 136,869 +560,545 -> 234,545 +973,821 -> 829,821 +287,699 -> 829,699 +790,114 -> 258,114 +639,79 -> 779,79 +774,655 -> 774,79 +332,340 -> 332,530 +894,583 -> 152,583 +242,884 -> 242,285 +126,928 -> 538,516 +10,989 -> 989,10 +336,310 -> 33,310 +881,238 -> 141,978 +239,930 -> 747,930 +964,864 -> 130,30 +116,25 -> 872,781 +925,915 -> 94,84 +923,56 -> 40,939 +545,446 -> 427,446 +200,512 -> 200,99 +723,929 -> 430,636 +728,550 -> 296,550 +52,293 -> 231,293 +123,557 -> 123,798 +468,513 -> 468,891 +866,322 -> 849,322 +893,265 -> 893,201 +711,811 -> 711,507 +431,304 -> 501,304 +79,766 -> 79,483 +705,115 -> 980,115 +966,20 -> 23,963 +807,224 -> 989,406 +114,370 -> 853,370 +508,552 -> 508,152 +924,58 -> 108,874 +928,754 -> 928,912 +757,802 -> 574,985 +874,256 -> 810,192 +814,404 -> 814,597 +32,957 -> 886,103 +693,917 -> 693,649 +984,28 -> 36,976 +615,898 -> 466,898 +34,334 -> 34,966 +741,305 -> 741,63 +817,961 -> 691,961 +431,85 -> 431,111 +709,72 -> 709,745 +185,549 -> 685,549 +928,972 -> 28,72 +721,958 -> 615,958 +287,417 -> 792,417 +335,156 -> 335,609 +789,80 -> 789,626 +866,889 -> 67,90 +113,538 -> 113,935 +375,682 -> 985,72 +598,492 -> 767,492 +61,750 -> 658,153 +681,673 -> 681,293 +672,766 -> 672,273 +513,335 -> 382,335 +818,60 -> 741,60 +361,561 -> 707,215 +460,487 -> 460,463 +190,705 -> 871,24 +43,309 -> 457,723 +267,394 -> 144,394 +316,141 -> 316,154 +818,617 -> 823,612 +542,46 -> 542,813 +569,480 -> 557,480 +914,807 -> 298,191 +942,315 -> 437,820 +598,598 -> 60,598 +296,344 -> 296,710 +281,153 -> 281,575 +978,834 -> 978,878 +194,700 -> 194,403 +429,533 -> 10,533 +789,156 -> 789,509 +195,357 -> 789,357 +216,217 -> 404,29 +865,890 -> 865,896 +153,590 -> 153,412 +695,177 -> 695,618 +306,74 -> 918,686 +298,627 -> 298,498 +671,657 -> 671,641 +112,806 -> 112,880 +693,770 -> 55,132 +192,743 -> 192,342 +982,63 -> 66,979 +262,858 -> 262,685 +696,114 -> 669,114 +974,507 -> 626,507 +361,168 -> 219,310 +721,18 -> 401,338 +801,39 -> 513,327 +816,13 -> 724,13 +901,215 -> 901,83 +165,146 -> 788,146 +425,662 -> 418,669 +670,92 -> 913,335 +87,988 -> 963,112 +501,53 -> 252,53 +583,709 -> 583,600 +852,327 -> 852,208 +943,409 -> 943,721 +712,69 -> 951,69 +519,768 -> 519,648 +969,46 -> 35,980 +235,626 -> 235,447 +213,861 -> 741,333 +103,797 -> 136,830 +770,108 -> 770,546 +320,623 -> 320,200 +889,169 -> 889,243 +401,673 -> 401,783 +178,736 -> 639,736 +819,883 -> 15,79 +279,509 -> 279,399 +203,947 -> 866,947 +564,57 -> 577,57 +558,366 -> 558,751 +922,425 -> 355,425 +457,597 -> 61,597 \ No newline at end of file