From: Brett Parker Date: Tue, 7 Dec 2021 09:11:16 +0000 (+0000) Subject: Day 5 X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2021.git/commitdiff_plain/c4b0490df9ba6212d7ac6caebbc56ae128d73d92?ds=inline Day 5 --- diff --git a/day05/example.txt b/day05/example.txt new file mode 100644 index 0000000..b258f68 --- /dev/null +++ b/day05/example.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 diff --git a/day05/input.txt b/day05/input.txt new file mode 100644 index 0000000..7e1d816 --- /dev/null +++ b/day05/input.txt @@ -0,0 +1,500 @@ +561,579 -> 965,175 +735,73 -> 316,73 +981,566 -> 981,11 +631,588 -> 631,910 +919,964 -> 70,115 +122,463 -> 261,602 +502,248 -> 774,248 +636,797 -> 797,958 +444,815 -> 444,554 +734,97 -> 734,210 +871,692 -> 368,692 +842,261 -> 606,261 +332,43 -> 206,43 +371,220 -> 18,573 +594,782 -> 127,782 +952,953 -> 72,73 +13,143 -> 755,885 +827,741 -> 827,168 +65,84 -> 839,858 +299,598 -> 299,588 +478,76 -> 607,76 +656,748 -> 61,153 +566,447 -> 857,447 +497,19 -> 310,206 +354,721 -> 538,721 +124,17 -> 124,339 +105,595 -> 843,595 +686,902 -> 919,669 +683,183 -> 776,183 +415,45 -> 415,942 +16,852 -> 843,25 +945,954 -> 23,32 +935,174 -> 485,624 +712,297 -> 792,297 +495,90 -> 497,90 +836,259 -> 836,988 +12,59 -> 916,963 +72,648 -> 72,185 +625,400 -> 507,282 +809,206 -> 809,538 +472,909 -> 472,213 +847,162 -> 189,162 +178,233 -> 178,777 +404,276 -> 404,516 +943,178 -> 400,721 +882,429 -> 882,920 +12,976 -> 900,88 +33,53 -> 958,53 +104,37 -> 609,542 +207,736 -> 800,143 +331,454 -> 331,715 +956,52 -> 26,982 +189,544 -> 189,368 +194,258 -> 451,258 +75,383 -> 75,642 +770,861 -> 915,716 +300,330 -> 300,683 +649,175 -> 344,175 +948,25 -> 42,931 +245,313 -> 748,816 +677,126 -> 677,204 +507,948 -> 507,88 +343,229 -> 343,677 +946,933 -> 472,459 +830,403 -> 830,660 +626,651 -> 626,213 +828,932 -> 828,487 +792,101 -> 216,101 +870,111 -> 50,931 +770,464 -> 290,944 +264,700 -> 941,23 +894,846 -> 630,846 +19,40 -> 957,978 +911,425 -> 393,425 +26,594 -> 26,475 +754,287 -> 754,302 +105,483 -> 807,483 +25,902 -> 684,243 +300,164 -> 532,396 +79,346 -> 79,423 +649,234 -> 649,331 +113,115 -> 962,964 +251,50 -> 531,330 +908,177 -> 908,817 +905,106 -> 905,570 +958,55 -> 41,972 +985,984 -> 62,61 +794,802 -> 328,802 +984,273 -> 370,887 +271,758 -> 237,792 +443,534 -> 835,926 +367,762 -> 367,521 +527,737 -> 717,737 +89,921 -> 89,299 +521,279 -> 108,279 +810,344 -> 760,394 +292,944 -> 292,941 +265,471 -> 265,949 +923,378 -> 415,886 +125,656 -> 434,656 +51,460 -> 51,542 +297,178 -> 297,587 +432,122 -> 241,122 +916,855 -> 451,390 +48,838 -> 636,838 +802,659 -> 784,641 +316,961 -> 417,961 +342,675 -> 568,675 +345,60 -> 352,60 +495,595 -> 284,806 +111,975 -> 815,271 +315,239 -> 315,333 +14,16 -> 930,932 +380,373 -> 380,346 +212,141 -> 212,691 +415,555 -> 651,319 +497,565 -> 96,164 +902,451 -> 735,451 +118,751 -> 521,751 +923,680 -> 613,370 +641,897 -> 233,489 +804,447 -> 259,447 +556,751 -> 968,751 +409,638 -> 409,65 +512,50 -> 432,130 +28,25 -> 988,985 +563,888 -> 563,502 +912,691 -> 950,691 +592,934 -> 592,433 +465,886 -> 408,886 +928,547 -> 671,290 +486,953 -> 496,943 +942,943 -> 42,43 +672,759 -> 266,759 +587,195 -> 763,371 +95,354 -> 687,946 +890,280 -> 734,124 +945,906 -> 945,168 +306,952 -> 306,257 +143,160 -> 810,827 +988,407 -> 167,407 +274,472 -> 817,472 +252,113 -> 430,291 +294,767 -> 176,767 +456,142 -> 423,142 +908,107 -> 176,839 +255,278 -> 447,86 +100,44 -> 969,913 +899,184 -> 899,986 +872,877 -> 47,52 +122,786 -> 228,786 +132,777 -> 312,777 +242,86 -> 36,86 +726,671 -> 690,635 +386,859 -> 790,455 +468,62 -> 636,62 +920,869 -> 920,790 +610,66 -> 633,43 +428,962 -> 428,717 +935,881 -> 278,224 +314,204 -> 458,204 +906,515 -> 712,515 +332,223 -> 744,223 +707,802 -> 842,802 +775,36 -> 946,207 +906,206 -> 142,970 +534,312 -> 534,622 +26,687 -> 26,417 +758,258 -> 688,188 +830,591 -> 830,286 +856,182 -> 924,182 +78,755 -> 78,880 +157,726 -> 157,904 +316,30 -> 752,30 +958,977 -> 10,29 +831,813 -> 284,813 +472,843 -> 989,326 +133,99 -> 133,537 +161,540 -> 846,540 +176,739 -> 78,739 +28,33 -> 969,974 +65,160 -> 879,974 +355,943 -> 355,685 +893,512 -> 225,512 +789,565 -> 789,81 +253,832 -> 943,832 +372,716 -> 372,605 +781,293 -> 702,293 +469,497 -> 469,149 +692,499 -> 933,258 +477,548 -> 477,849 +131,894 -> 131,483 +934,765 -> 13,765 +830,55 -> 436,449 +615,496 -> 531,412 +78,634 -> 78,115 +205,586 -> 205,661 +871,742 -> 871,590 +218,223 -> 218,741 +56,897 -> 872,81 +30,886 -> 728,188 +369,877 -> 369,963 +202,55 -> 930,783 +251,131 -> 251,749 +498,313 -> 498,126 +688,876 -> 188,876 +147,968 -> 607,508 +400,693 -> 235,693 +206,853 -> 206,211 +401,158 -> 187,158 +35,957 -> 973,19 +517,822 -> 517,282 +836,492 -> 433,895 +751,114 -> 100,114 +80,359 -> 710,359 +302,871 -> 812,871 +853,159 -> 853,548 +351,663 -> 451,763 +895,260 -> 307,848 +369,455 -> 812,455 +936,594 -> 393,51 +55,696 -> 972,696 +422,804 -> 192,804 +110,893 -> 120,883 +747,645 -> 747,181 +636,344 -> 636,88 +15,949 -> 543,421 +985,948 -> 51,14 +164,435 -> 164,27 +30,966 -> 965,31 +600,472 -> 985,472 +119,161 -> 91,161 +180,521 -> 180,802 +98,70 -> 449,70 +157,190 -> 613,190 +816,501 -> 338,23 +387,219 -> 387,75 +115,579 -> 115,734 +26,153 -> 26,450 +733,783 -> 462,783 +19,893 -> 759,893 +646,726 -> 286,726 +631,139 -> 631,664 +490,668 -> 490,231 +282,958 -> 282,816 +338,55 -> 898,55 +891,420 -> 443,420 +222,917 -> 222,759 +302,430 -> 465,430 +946,49 -> 946,753 +101,971 -> 879,193 +329,122 -> 888,122 +524,714 -> 154,344 +89,769 -> 552,306 +749,680 -> 749,536 +161,510 -> 161,317 +143,608 -> 543,608 +677,526 -> 658,526 +348,956 -> 512,956 +851,973 -> 358,480 +850,247 -> 56,247 +284,274 -> 381,274 +855,944 -> 101,190 +338,102 -> 467,102 +225,875 -> 225,303 +120,944 -> 945,119 +485,386 -> 305,386 +321,866 -> 627,866 +44,207 -> 44,244 +514,763 -> 514,578 +617,930 -> 617,371 +963,838 -> 451,326 +813,904 -> 174,265 +901,983 -> 512,594 +979,929 -> 527,477 +533,383 -> 59,857 +871,724 -> 828,767 +174,133 -> 174,75 +686,911 -> 15,911 +361,406 -> 440,327 +759,284 -> 275,284 +231,431 -> 203,431 +759,673 -> 839,673 +914,897 -> 914,937 +598,643 -> 598,722 +697,297 -> 697,55 +735,918 -> 735,81 +375,538 -> 375,403 +868,86 -> 170,784 +390,142 -> 390,553 +569,564 -> 569,800 +514,276 -> 717,73 +756,768 -> 182,768 +349,428 -> 349,364 +370,386 -> 370,621 +191,224 -> 191,447 +672,983 -> 209,983 +281,854 -> 872,854 +898,959 -> 898,207 +961,597 -> 561,197 +965,837 -> 338,210 +120,675 -> 914,675 +401,661 -> 401,167 +706,536 -> 706,598 +856,337 -> 283,910 +980,535 -> 156,535 +941,888 -> 179,126 +585,47 -> 933,395 +481,435 -> 481,818 +989,978 -> 45,34 +693,332 -> 693,528 +863,605 -> 507,961 +586,429 -> 252,429 +37,61 -> 962,986 +952,26 -> 16,962 +926,492 -> 863,492 +661,110 -> 279,110 +524,879 -> 176,879 +298,424 -> 858,984 +498,627 -> 498,963 +468,608 -> 256,820 +61,491 -> 472,491 +38,922 -> 938,22 +361,938 -> 361,46 +567,981 -> 567,973 +399,575 -> 902,72 +618,740 -> 818,540 +79,115 -> 929,965 +612,972 -> 612,698 +217,626 -> 474,369 +961,593 -> 961,70 +287,450 -> 287,676 +703,45 -> 871,45 +756,778 -> 410,778 +960,970 -> 42,52 +254,513 -> 554,813 +620,339 -> 601,320 +650,344 -> 650,33 +597,349 -> 597,491 +761,106 -> 891,236 +323,846 -> 883,846 +678,190 -> 134,190 +400,848 -> 400,271 +198,68 -> 463,68 +522,805 -> 522,445 +979,134 -> 719,134 +584,493 -> 584,471 +635,10 -> 635,768 +867,795 -> 31,795 +157,824 -> 939,42 +354,381 -> 653,381 +45,628 -> 45,860 +959,870 -> 808,719 +587,810 -> 291,810 +248,34 -> 83,199 +656,505 -> 656,388 +966,90 -> 966,934 +957,834 -> 267,144 +690,367 -> 447,367 +153,948 -> 153,510 +467,260 -> 467,332 +69,43 -> 931,905 +913,92 -> 913,827 +226,504 -> 226,615 +21,737 -> 21,819 +391,289 -> 391,341 +566,639 -> 723,639 +160,824 -> 167,824 +77,69 -> 379,371 +756,864 -> 756,960 +315,569 -> 927,569 +964,864 -> 964,167 +136,277 -> 136,184 +524,450 -> 898,450 +971,903 -> 222,903 +962,958 -> 70,66 +525,570 -> 45,90 +164,352 -> 164,433 +703,61 -> 668,61 +289,136 -> 768,136 +50,65 -> 861,876 +676,199 -> 676,411 +294,370 -> 56,132 +299,840 -> 620,840 +845,776 -> 845,66 +61,18 -> 909,866 +738,670 -> 738,438 +974,618 -> 974,169 +669,800 -> 607,800 +935,427 -> 537,427 +441,898 -> 441,462 +490,768 -> 490,239 +275,788 -> 947,116 +650,412 -> 493,255 +382,89 -> 950,89 +865,194 -> 865,266 +977,677 -> 459,677 +128,18 -> 985,875 +199,646 -> 199,611 +666,149 -> 666,504 +129,539 -> 129,266 +332,364 -> 332,211 +619,132 -> 619,580 +426,69 -> 426,236 +123,334 -> 22,334 +792,57 -> 449,57 +33,976 -> 987,22 +221,614 -> 809,614 +245,379 -> 280,379 +895,238 -> 895,500 +957,423 -> 119,423 +917,50 -> 346,621 +183,263 -> 708,263 +452,174 -> 452,216 +404,681 -> 287,798 +931,54 -> 474,54 +815,264 -> 151,928 +736,507 -> 295,66 +200,160 -> 901,861 +709,975 -> 100,366 +126,468 -> 393,201 +822,627 -> 287,92 +101,285 -> 176,285 +250,581 -> 250,799 +257,446 -> 257,927 +436,423 -> 640,219 +468,229 -> 987,748 +394,245 -> 501,245 +37,931 -> 684,931 +123,54 -> 123,453 +75,364 -> 758,364 +256,62 -> 143,62 +933,299 -> 933,410 +313,887 -> 313,34 +453,833 -> 457,833 +614,87 -> 614,816 +48,113 -> 48,646 +977,571 -> 977,967 +982,989 -> 32,39 +773,749 -> 239,215 +983,989 -> 218,224 +885,606 -> 169,606 +63,53 -> 63,29 +96,870 -> 820,870 +544,895 -> 389,740 +61,246 -> 296,246 +812,82 -> 53,841 +749,399 -> 749,935 +882,168 -> 274,168 +566,673 -> 499,606 +572,558 -> 806,792 +923,79 -> 923,844 +875,416 -> 875,112 +393,18 -> 393,149 +389,844 -> 147,844 +207,157 -> 207,949 +467,644 -> 728,644 +36,29 -> 587,29 +448,370 -> 448,247 +43,283 -> 43,122 +709,837 -> 709,265 +294,273 -> 98,273 +625,869 -> 26,270 +156,26 -> 145,26 +393,544 -> 393,456 +708,536 -> 64,536 +324,519 -> 324,950 +138,788 -> 533,788 +117,818 -> 254,681 +953,766 -> 306,119 +669,325 -> 669,940 +421,416 -> 715,416 +886,101 -> 886,371 +786,656 -> 786,169 +123,641 -> 622,142 +658,504 -> 985,177 +440,505 -> 74,139 +300,616 -> 300,473 +265,729 -> 983,11 +501,396 -> 274,396 +877,123 -> 113,887 +544,78 -> 544,604 +287,944 -> 287,391 +766,854 -> 97,185 +959,49 -> 127,881 +715,727 -> 715,647 +339,557 -> 339,610 +19,133 -> 87,133 +872,94 -> 932,94 +966,935 -> 95,64 +137,942 -> 918,161 +162,718 -> 598,282 +256,18 -> 256,47 +597,658 -> 314,941 +817,685 -> 137,685 +20,14 -> 988,982 +970,916 -> 100,46 +672,152 -> 672,842 +468,825 -> 468,911 +673,731 -> 267,325 diff --git a/day05/task.txt b/day05/task.txt new file mode 100644 index 0000000..b1c945c --- /dev/null +++ b/day05/task.txt @@ -0,0 +1,73 @@ +--- Day 5: Hydrothermal Venture --- +You come across a field of hydrothermal vents on the ocean floor! These vents constantly produce large, opaque clouds, so it would be best to avoid them if possible. + +They tend to form in lines; the submarine helpfully produces a list of nearby lines of vents (your puzzle input) for you to review. For example: + +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 +Each line of vents is given as a line segment in the format x1,y1 -> x2,y2 where x1,y1 are the coordinates of one end the line segment and x2,y2 are the coordinates of the other end. These line segments include the points at both ends. In other words: + +An entry like 1,1 -> 1,3 covers points 1,1, 1,2, and 1,3. +An entry like 9,7 -> 7,7 covers points 9,7, 8,7, and 7,7. +For now, only consider horizontal and vertical lines: lines where either x1 = x2 or y1 = y2. + +So, the horizontal and vertical lines from the above list would produce the following diagram: + +.......1.. +..1....1.. +..1....1.. +.......1.. +.112111211 +.......... +.......... +.......... +.......... +222111.... +In this diagram, the top left corner is 0,0 and the bottom right corner is 9,9. Each position is shown as the number of lines which cover that point or . if no line covers that point. The top-left pair of 1s, for example, comes from 2,2 -> 2,1; the very bottom row is formed by the overlapping lines 0,9 -> 5,9 and 0,9 -> 2,9. + +To avoid the most dangerous areas, you need to determine the number of points where at least two lines overlap. In the above example, this is anywhere in the diagram with a 2 or larger - a total of 5 points. + +Consider only horizontal and vertical lines. At how many points do at least two lines overlap? + +Your puzzle answer was 6005. + +--- Part Two --- +Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider diagonal lines. + +Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words: + +An entry like 1,1 -> 3,3 covers points 1,1, 2,2, and 3,3. +An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,9. +Considering all lines from the above example would now produce the following diagram: + +1.1....11. +.111...2.. +..2.1.111. +...1.2.2.. +.112313211 +...1.2.... +..1...1... +.1.....1.. +1.......1. +222111.... +You still need to determine the number of points where at least two lines overlap. In the above example, this is still anywhere in the diagram with a 2 or larger - now a total of 12 points. + +Consider all of the lines. At how many points do at least two lines overlap? + +Your puzzle answer was 23864. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day05/vents.sh b/day05/vents.sh new file mode 100755 index 0000000..5d4eecf --- /dev/null +++ b/day05/vents.sh @@ -0,0 +1,175 @@ +#!/bin/bash + +set -e +set -u + +filename="${1:-example.txt}" + +exec 3<"$filename" + +declare -a lines + +max_x=0 +max_y=0 +x1=0 +y1=0 +x2=0 +y2=0 + +declare -a grid + +get_coords() { + coords_line="$1" + local -n lx1=$2 + local -n ly1=$3 + local -n lx2=$4 + local -n ly2=$5 + + local first_x_y=${coords_line% -> *} + local second_x_y=${coords_line#* -> } + lx1=${first_x_y%,*} + ly1=${first_x_y#*,} + lx2=${second_x_y%,*} + ly2=${second_x_y#*,} +} + +generate_grid() { + local max_x=$1 + local max_y=$2 + local -n grid_arr=$3 + + for (( i=0; i<$max_y; i++ )); do + for (( j=0; j<$max_x; j++ )); do + grid_arr+=(0) + done + done +} + +display_grid() { + local max_x=$1 + local -n grid_arr=$2 + + local cur_pos=0 + + while (( $cur_pos < ${#grid_arr[@]} )); do + case ${grid_arr[$cur_pos]} in + 0) + echo -n . + ;; + *) + echo -n ${grid_arr[$cur_pos]} + ;; + esac + ((cur_pos+=1)) + if [ $((cur_pos % ($max_x))) -eq 0 ] && [ $cur_pos -gt 0 ]; then + echo + fi + done +} + +get_intersections() { + local -n grid_arr=$1 + local count=0 + + for num in "${grid_arr[@]}"; do + if [ $num -gt 1 ]; then + ((count+=1)) + fi + done + + echo $count +} + +while read -u3 line; do + lines+=("$line") + get_coords "$line" x1 y1 x2 y2 + if [ $x1 -gt $max_x ]; then + max_x=$x1 + fi + if [ $x2 -gt $max_x ]; then + max_x=$x2 + fi + if [ $y1 -gt $max_y ]; then + max_y=$y1 + fi + if [ $y2 -gt $max_y ]; then + max_y=$y2 + fi +done + +# because we're 0 based, max_x and max_y want 1 added to make the maths work +((max_x+=1)) +((max_y+=1)) + +echo "Size of box: $max_x, $max_y" + +generate_grid $max_x $max_y grid + +for coords in "${lines[@]}"; do + get_coords "$coords" x1 y1 x2 y2 + if [ $x1 -eq $x2 ] || [ $y1 -eq $y2 ]; then + if [ $x1 -eq $x2 ]; then + # we're working on a vertical line + if [ $y1 -gt $y2 ]; then + ytemp=$y1 + y1=$y2 + y2=$ytemp + fi + for (( i=$y1; i<=$y2; i+=1 )); do + cur_pos=$(($x1 + ($max_x * $i))) + ((grid[$cur_pos]+=1)) + done + elif [ $y1 -eq $y2 ]; then + direction=1 + # we're working on a horizonal line + if [ $x1 -gt $x2 ]; then + xtemp=$x1 + x1=$x2 + x2=$xtemp + fi + for (( i=$x1; i<=$x2; i+=1 )); do + cur_pos=$(($i + ($max_x * $y1))) + ((grid[$cur_pos]+=1)) + done + fi + fi +done + +#display_grid $max_x grid + +intersection_count=$(get_intersections grid) + +echo "Intersections (h+v): $intersection_count" + +# do the diagonals +for coords in "${lines[@]}"; do + get_coords "$coords" x1 y1 x2 y2 + if [ $x1 -ne $x2 ] && [ $y1 -ne $y2 ]; then + # we've got a diagonal line, woo + if [ $y1 -gt $y2 ]; then + # swap everything + temp=$y1 + y1=$y2 + y2=$temp + temp=$x1 + x1=$x2 + x2=$temp + fi + cur_x=$x1 + for (( i=$y1; i<=$y2; i++ )); do + cur_pos=$((cur_x + ($max_x * i))) + ((grid[$cur_pos]+=1)) + if [ $x1 -gt $x2 ]; then + ((cur_x-=1)) || cur_x=0 + else + ((cur_x+=1)) + fi + done + fi +done + +#display_grid $max_x grid + +intersection_count=$(get_intersections grid) + +echo "Intersections (h+v+d): $intersection_count"