diff --git a/05/input.txt b/05/input.txt new file mode 100644 index 0000000..3c1470a --- /dev/null +++ b/05/input.txt @@ -0,0 +1,500 @@ +644,38 -> 644,265 +941,468 -> 941,89 +807,552 -> 618,363 +896,510 -> 896,744 +227,909 -> 227,745 +24,66 -> 946,988 +563,529 -> 563,270 +894,707 -> 359,172 +146,253 -> 146,569 +544,683 -> 140,683 +755,612 -> 755,463 +394,320 -> 891,320 +68,616 -> 68,628 +801,770 -> 801,942 +650,290 -> 580,360 +376,18 -> 181,213 +285,391 -> 805,911 +57,153 -> 501,153 +686,301 -> 686,316 +81,971 -> 683,369 +75,456 -> 75,355 +769,59 -> 769,81 +49,958 -> 49,718 +492,754 -> 492,256 +606,133 -> 391,133 +134,683 -> 134,656 +195,463 -> 658,926 +982,498 -> 982,927 +841,122 -> 889,74 +968,891 -> 968,189 +104,941 -> 104,868 +801,450 -> 272,979 +212,749 -> 212,366 +880,928 -> 880,757 +503,483 -> 283,263 +792,924 -> 792,540 +58,87 -> 905,934 +275,661 -> 36,422 +475,654 -> 466,645 +34,946 -> 969,11 +725,664 -> 725,150 +837,680 -> 837,952 +109,987 -> 644,452 +860,984 -> 860,691 +672,800 -> 107,235 +216,888 -> 301,888 +399,676 -> 718,357 +957,544 -> 784,544 +677,743 -> 633,743 +491,866 -> 77,452 +967,949 -> 45,27 +659,699 -> 659,27 +987,116 -> 696,116 +465,847 -> 309,847 +353,19 -> 353,627 +265,713 -> 592,386 +541,765 -> 461,765 +21,409 -> 895,409 +950,149 -> 766,149 +856,889 -> 895,889 +335,962 -> 485,812 +425,774 -> 400,749 +71,932 -> 989,14 +484,974 -> 44,974 +86,797 -> 86,470 +876,962 -> 876,384 +45,631 -> 169,507 +161,789 -> 523,427 +791,640 -> 791,581 +415,170 -> 835,590 +299,275 -> 988,964 +105,233 -> 231,359 +202,226 -> 202,958 +814,717 -> 618,521 +185,442 -> 559,68 +26,149 -> 665,149 +673,369 -> 247,795 +171,963 -> 171,46 +689,801 -> 94,206 +619,243 -> 619,637 +426,245 -> 660,11 +47,503 -> 47,179 +341,363 -> 487,217 +371,774 -> 371,871 +781,794 -> 781,180 +391,632 -> 92,632 +517,150 -> 517,715 +903,10 -> 18,895 +34,500 -> 426,500 +82,955 -> 886,151 +142,297 -> 142,527 +60,965 -> 36,965 +250,807 -> 372,685 +227,393 -> 610,776 +269,893 -> 269,556 +969,223 -> 611,223 +255,92 -> 194,92 +220,233 -> 958,233 +144,209 -> 144,979 +48,413 -> 48,195 +209,151 -> 648,590 +867,648 -> 871,644 +499,555 -> 807,555 +571,729 -> 865,435 +683,151 -> 485,151 +803,26 -> 803,691 +533,921 -> 300,688 +625,695 -> 483,553 +719,370 -> 139,950 +981,17 -> 16,982 +367,617 -> 367,636 +249,644 -> 835,644 +260,825 -> 260,519 +275,144 -> 275,315 +767,314 -> 962,119 +76,625 -> 76,166 +403,680 -> 313,680 +977,21 -> 16,982 +578,753 -> 271,753 +212,358 -> 477,623 +596,220 -> 868,220 +67,797 -> 935,797 +181,107 -> 181,379 +741,332 -> 419,10 +732,827 -> 732,989 +835,111 -> 436,510 +480,328 -> 480,682 +327,673 -> 327,70 +911,547 -> 833,625 +944,509 -> 874,579 +169,340 -> 731,902 +156,842 -> 825,173 +976,40 -> 51,965 +199,416 -> 916,416 +51,970 -> 792,229 +14,577 -> 501,577 +246,464 -> 246,289 +641,464 -> 100,464 +984,11 -> 43,952 +548,36 -> 156,428 +519,799 -> 519,43 +332,364 -> 332,774 +85,123 -> 85,753 +778,12 -> 40,750 +507,56 -> 507,840 +973,632 -> 670,632 +895,928 -> 37,70 +661,784 -> 661,718 +114,25 -> 938,849 +428,752 -> 428,543 +357,270 -> 957,870 +27,70 -> 899,942 +85,780 -> 747,780 +717,565 -> 951,565 +191,748 -> 651,748 +679,301 -> 679,417 +150,266 -> 150,184 +774,964 -> 64,254 +34,248 -> 34,279 +782,610 -> 802,610 +109,146 -> 822,859 +825,848 -> 825,666 +251,718 -> 615,718 +912,722 -> 289,722 +729,579 -> 729,889 +214,756 -> 214,716 +58,497 -> 58,927 +62,55 -> 742,55 +110,429 -> 110,154 +55,794 -> 638,794 +182,96 -> 676,96 +632,334 -> 202,764 +36,541 -> 36,397 +191,819 -> 191,277 +971,491 -> 839,491 +849,561 -> 538,250 +176,523 -> 10,689 +162,638 -> 717,638 +132,843 -> 646,329 +873,67 -> 873,389 +167,631 -> 167,473 +49,337 -> 465,337 +550,429 -> 438,429 +305,720 -> 476,720 +547,636 -> 547,902 +21,627 -> 49,627 +286,70 -> 989,70 +87,930 -> 896,121 +659,916 -> 200,916 +234,589 -> 234,308 +530,962 -> 90,962 +366,478 -> 676,788 +284,520 -> 284,546 +580,74 -> 146,508 +561,977 -> 561,237 +85,150 -> 804,869 +740,850 -> 159,269 +458,705 -> 458,639 +969,563 -> 381,563 +139,439 -> 139,263 +135,971 -> 908,198 +440,632 -> 102,632 +446,549 -> 446,734 +24,49 -> 212,237 +692,882 -> 527,882 +156,895 -> 156,239 +359,246 -> 359,603 +12,349 -> 604,941 +375,161 -> 732,161 +674,287 -> 322,287 +902,957 -> 328,383 +352,185 -> 352,766 +222,205 -> 222,419 +769,815 -> 769,739 +83,147 -> 880,147 +136,148 -> 136,279 +807,241 -> 439,241 +464,240 -> 478,254 +37,839 -> 646,839 +351,316 -> 639,604 +449,492 -> 449,125 +69,892 -> 933,28 +565,161 -> 598,161 +147,802 -> 905,44 +809,861 -> 38,90 +34,80 -> 34,538 +896,405 -> 582,405 +474,201 -> 893,201 +289,64 -> 975,750 +378,785 -> 375,782 +709,472 -> 357,472 +17,14 -> 987,984 +625,46 -> 647,46 +20,84 -> 914,978 +364,811 -> 397,811 +395,726 -> 628,726 +831,89 -> 593,89 +370,562 -> 827,562 +278,371 -> 609,40 +904,529 -> 391,16 +232,829 -> 232,63 +927,121 -> 194,854 +547,280 -> 547,895 +292,904 -> 292,897 +99,525 -> 99,868 +371,44 -> 643,44 +942,74 -> 986,118 +708,179 -> 110,179 +485,637 -> 713,637 +477,512 -> 102,137 +636,657 -> 727,657 +686,564 -> 256,564 +636,169 -> 377,169 +627,943 -> 627,703 +987,795 -> 987,758 +364,827 -> 642,827 +613,694 -> 864,945 +910,521 -> 777,654 +119,394 -> 22,394 +594,439 -> 515,360 +177,11 -> 177,73 +456,628 -> 456,467 +246,214 -> 594,562 +47,790 -> 47,491 +788,21 -> 788,343 +136,847 -> 136,543 +958,302 -> 733,302 +50,981 -> 923,108 +298,179 -> 298,921 +962,644 -> 93,644 +988,671 -> 988,258 +646,140 -> 646,852 +721,264 -> 721,563 +972,407 -> 684,119 +245,422 -> 401,422 +16,494 -> 16,16 +564,72 -> 361,72 +436,390 -> 166,120 +511,571 -> 241,571 +259,215 -> 259,975 +648,841 -> 898,841 +918,881 -> 438,401 +458,561 -> 752,855 +791,192 -> 319,192 +383,929 -> 217,929 +733,26 -> 527,26 +620,160 -> 620,734 +818,181 -> 61,181 +39,21 -> 927,909 +952,208 -> 952,749 +194,55 -> 304,55 +519,673 -> 519,875 +730,919 -> 733,919 +963,269 -> 933,299 +707,112 -> 178,112 +924,349 -> 264,349 +910,94 -> 96,94 +747,289 -> 89,947 +164,956 -> 164,655 +264,300 -> 753,789 +207,672 -> 207,488 +243,838 -> 812,269 +455,320 -> 425,320 +59,964 -> 59,841 +350,373 -> 526,549 +604,683 -> 604,83 +537,281 -> 537,933 +737,634 -> 737,28 +92,909 -> 725,276 +859,335 -> 859,487 +605,495 -> 371,495 +783,155 -> 783,930 +388,591 -> 388,133 +374,634 -> 472,634 +963,914 -> 64,15 +57,435 -> 140,435 +759,619 -> 105,619 +326,501 -> 326,821 +942,136 -> 414,136 +490,376 -> 490,260 +377,59 -> 377,773 +894,169 -> 99,964 +350,511 -> 726,511 +787,728 -> 787,750 +688,11 -> 688,68 +107,514 -> 183,514 +861,12 -> 22,851 +149,49 -> 149,713 +826,737 -> 890,737 +299,307 -> 299,549 +251,648 -> 862,37 +898,85 -> 412,85 +936,168 -> 845,168 +278,677 -> 302,677 +493,878 -> 493,133 +539,205 -> 539,522 +412,495 -> 412,929 +334,605 -> 334,817 +68,462 -> 462,856 +234,134 -> 234,942 +293,823 -> 678,823 +852,874 -> 158,874 +859,612 -> 435,612 +10,876 -> 613,273 +370,13 -> 118,13 +616,774 -> 14,172 +193,543 -> 81,543 +784,179 -> 14,949 +324,533 -> 153,533 +15,976 -> 976,15 +943,52 -> 20,975 +100,34 -> 671,605 +140,552 -> 140,27 +448,497 -> 186,497 +734,355 -> 734,933 +544,131 -> 259,131 +193,84 -> 663,84 +190,949 -> 833,306 +430,34 -> 328,34 +21,46 -> 483,508 +621,202 -> 98,202 +196,874 -> 889,181 +106,217 -> 850,961 +701,891 -> 488,678 +21,597 -> 894,597 +249,21 -> 249,614 +887,808 -> 887,948 +255,711 -> 913,53 +173,447 -> 173,78 +956,224 -> 956,747 +513,882 -> 48,417 +772,591 -> 930,591 +976,98 -> 86,988 +264,67 -> 264,931 +987,982 -> 48,43 +494,514 -> 494,416 +867,785 -> 556,474 +350,902 -> 257,902 +978,977 -> 978,735 +312,240 -> 371,240 +890,112 -> 890,662 +753,327 -> 427,653 +83,888 -> 927,44 +142,480 -> 12,480 +965,39 -> 85,919 +158,926 -> 158,801 +439,22 -> 439,860 +10,26 -> 765,781 +775,838 -> 775,732 +871,607 -> 871,288 +337,850 -> 337,533 +89,581 -> 89,582 +524,977 -> 524,88 +473,283 -> 639,449 +23,324 -> 23,372 +671,486 -> 278,879 +266,98 -> 989,98 +51,947 -> 634,947 +47,18 -> 47,43 +95,239 -> 609,753 +953,34 -> 71,916 +243,685 -> 243,427 +642,537 -> 304,199 +545,41 -> 914,41 +112,860 -> 112,159 +12,12 -> 989,989 +622,907 -> 648,907 +230,983 -> 230,941 +111,868 -> 852,127 +315,895 -> 799,411 +771,533 -> 584,720 +65,492 -> 657,492 +820,315 -> 820,627 +710,554 -> 829,554 +100,967 -> 877,190 +530,733 -> 530,202 +880,28 -> 83,825 +914,448 -> 494,28 +886,344 -> 966,344 +782,923 -> 857,848 +597,632 -> 597,946 +366,789 -> 366,96 +568,834 -> 187,834 +451,687 -> 224,687 +123,12 -> 914,803 +734,42 -> 526,42 +844,116 -> 844,521 +744,976 -> 744,792 +337,837 -> 617,837 +848,139 -> 848,571 +853,264 -> 112,264 +986,965 -> 92,71 +888,892 -> 53,57 +150,342 -> 150,516 +457,264 -> 796,264 +544,595 -> 544,406 +243,118 -> 482,357 +138,804 -> 138,646 +99,80 -> 99,34 +442,883 -> 442,165 +428,72 -> 428,826 +450,815 -> 901,364 +852,516 -> 852,384 +13,969 -> 972,10 +921,50 -> 32,939 +969,470 -> 969,539 +691,238 -> 763,238 +171,733 -> 823,81 +722,963 -> 21,262 +849,203 -> 849,151 +12,99 -> 884,971 +903,240 -> 148,240 +633,140 -> 633,202 +194,393 -> 747,393 +748,43 -> 126,665 +182,155 -> 182,298 +668,814 -> 668,306 +619,384 -> 255,384 +166,341 -> 795,970 +898,870 -> 57,29 +976,148 -> 976,244 +386,525 -> 662,249 +719,67 -> 131,655 +419,847 -> 188,847 +953,823 -> 743,823 +307,299 -> 307,78 +948,253 -> 756,253 +926,938 -> 46,58 +982,618 -> 652,618 +539,742 -> 539,177 +531,974 -> 531,781 +880,922 -> 283,325 +707,257 -> 707,673 +327,308 -> 930,911 +526,170 -> 980,624 +445,653 -> 445,986 +842,737 -> 754,737 +847,982 -> 16,151 +137,426 -> 292,271 +126,556 -> 50,556 +168,766 -> 168,767 +152,46 -> 363,46 +50,381 -> 760,381 +43,943 -> 43,374 +45,964 -> 956,53 +37,403 -> 576,403 +130,317 -> 576,763 +882,840 -> 179,137 +572,608 -> 572,274 +874,977 -> 32,135 +794,56 -> 730,120 +812,841 -> 812,440 +111,66 -> 137,92 +255,287 -> 255,787 +121,137 -> 121,63 +81,811 -> 81,586 +303,365 -> 910,972 +177,134 -> 962,919 +405,41 -> 405,645 +219,934 -> 441,934 \ No newline at end of file diff --git a/05/main.go b/05/main.go new file mode 100644 index 0000000..18fae1a --- /dev/null +++ b/05/main.go @@ -0,0 +1,103 @@ +package main + +import ( + "log" + "os" + "regexp" + "strconv" + "strings" + + "github.com/thoas/go-funk" +) + +func main() { + inputBytes, _ := os.ReadFile("input.txt") + inputs := strings.Split(string(inputBytes), "\n") + + log.Println("Puzzle 1:", puzzle1(inputs)) + log.Println("Puzzle 2:", puzzle2(inputs)) +} + +func puzzle1(input []string) int { + return do(input, false) +} + +func puzzle2(input []string) int { + return do(input, true) +} + +type lineEnd struct { + x, y int +} + +type line struct { + start, end lineEnd +} + +func do(input []string, puzzle2 bool) int { + // Parse input into lines of form "x1,y1 -> x2,y2" + lineRegex := regexp.MustCompile(`(\d+),(\d+)\s\->\s(\d+),(\d+)`) + lines := []line{} + for _, inputRow := range input { + lineMatch := lineRegex.FindStringSubmatch(inputRow) + x1, _ := strconv.Atoi(lineMatch[1]) + y1, _ := strconv.Atoi(lineMatch[2]) + x2, _ := strconv.Atoi(lineMatch[3]) + y2, _ := strconv.Atoi(lineMatch[4]) + lines = append(lines, line{ + start: lineEnd{x1, y1}, + end: lineEnd{x2, y2}, + }) + } + // Get maximum x and y values + maxX, maxY := 0, 0 + for _, l := range lines { + if l.start.x > maxX { + maxX = l.start.x + } + if l.start.y > maxY { + maxY = l.start.y + } + if l.end.x > maxX { + maxX = l.end.x + } + if l.end.y > maxY { + maxY = l.end.y + } + } + // Create a 2D array of zeros + diagram := make([][]int, maxY+1) + for i := range diagram { + diagram[i] = make([]int, maxX+1) + } + // For each line, increment the value of the cells it crosses + for _, l := range lines { + if l.start.x == l.end.x || l.start.y == l.end.y { + // Vertical line or horizontal line + for x := funk.MinInt([]int{l.start.x, l.end.x}); x <= funk.MaxInt([]int{l.start.x, l.end.x}); x++ { + for y := funk.MinInt([]int{l.start.y, l.end.y}); y <= funk.MaxInt([]int{l.start.y, l.end.y}); y++ { + diagram[y][x]++ + } + } + } else if puzzle2 && (l.start.x-l.end.x == l.start.y-l.end.y || l.start.x-l.end.x == l.end.y-l.start.y) { + // 45-degree diagonal line + xDir := funk.ShortIf(l.start.x > l.end.x, -1, 1).(int) + yDir := funk.ShortIf(l.start.y > l.end.y, -1, 1).(int) + x := l.start.x + for y := l.start.y; y != l.end.y+yDir; y += yDir { + diagram[y][x]++ + x += xDir + } + } + } + // Count the number of cells that have more than one line crossing them + count := 0 + for _, row := range diagram { + for _, cell := range row { + if cell > 1 { + count++ + } + } + } + return count +} diff --git a/05/main_test.go b/05/main_test.go new file mode 100644 index 0000000..a253fb8 --- /dev/null +++ b/05/main_test.go @@ -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") + inputs := strings.Split(string(inputBytes), "\n") + + assert.Equal(t, 5, puzzle1(inputs)) + assert.Equal(t, 12, puzzle2(inputs)) +} diff --git a/05/test.txt b/05/test.txt new file mode 100644 index 0000000..1d4e36d --- /dev/null +++ b/05/test.txt @@ -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