From c41309a8df5fc794475e3a157bde068efd05f878 Mon Sep 17 00:00:00 2001 From: Akumatic Date: Sun, 5 Dec 2021 23:27:14 +0100 Subject: [PATCH] 2021 Day 05 --- 2021/05/README.md | 78 +++++++ 2021/05/code.py | 38 ++++ 2021/05/input.txt | 500 +++++++++++++++++++++++++++++++++++++++++ 2021/05/solution.txt | 2 + 2021/05/test_code.py | 14 ++ 2021/05/test_input.txt | 10 + 2021/README.md | 2 +- 7 files changed, 643 insertions(+), 1 deletion(-) create mode 100644 2021/05/README.md create mode 100644 2021/05/code.py create mode 100644 2021/05/input.txt create mode 100644 2021/05/solution.txt create mode 100644 2021/05/test_code.py create mode 100644 2021/05/test_input.txt diff --git a/2021/05/README.md b/2021/05/README.md new file mode 100644 index 0000000..31a69b2 --- /dev/null +++ b/2021/05/README.md @@ -0,0 +1,78 @@ +# 2021 Day 05: Hydrothermal Venture +Copyright (c) Eric Wastl +#### [Direct Link](https://adventofcode.com/2021/day/05) + +## Part 1 + +You come across a field of [hydrothermal vents](https://en.wikipedia.org/wiki/Hydrothermal_vent) 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 `1`s, 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?** + +## Part 2 + +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?** \ No newline at end of file diff --git a/2021/05/code.py b/2021/05/code.py new file mode 100644 index 0000000..c7182b3 --- /dev/null +++ b/2021/05/code.py @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2021 Akumatic +# +# https://adventofcode.com/2021/day/05 + +def read_file(filename: str = "input.txt") -> list: + with open(f"{__file__.rstrip('code.py')}{filename}", "r") as f: + lines = [line.split(" -> ") for line in f.read().strip().split("\n")] + return [[(int(s[0]), int(s[1])) for s in (x.split(",") for x in line)] for line in lines] + +def count_crossings(vals: list, allow_diagonal: bool = False) -> int: + segments = dict() + for val in vals: + x1, x2, y1, y2 = val[0][0], val[1][0], val[0][1], val[1][1] + x_dir = 1 if x1 < x2 else -1 if x1 > x2 else 0 + y_dir = 1 if y1 < y2 else -1 if y1 > y2 else 0 + dist = max(abs(x2 - x1), abs(y2 - y1)) + cond = True if allow_diagonal else (x_dir == 0 or y_dir == 0) + tmp = [(x1 + n*x_dir, y1 + n*y_dir) for n in range(dist + 1) if cond] + + for segment in tmp: + if segment not in segments: + segments[segment] = 1 + else: + segments[segment] += 1 + + return sum(1 for x in segments if segments[x] > 1) + +def part1(vals: list) -> int: + return count_crossings(vals) + +def part2(vals: list) -> int: + return count_crossings(vals, allow_diagonal=True) + +if __name__ == "__main__": + vals = read_file() + print(f"Part 1: {part1(vals)}") + print(f"Part 2: {part2(vals)}") \ No newline at end of file diff --git a/2021/05/input.txt b/2021/05/input.txt new file mode 100644 index 0000000..7d9c95a --- /dev/null +++ b/2021/05/input.txt @@ -0,0 +1,500 @@ +556,286 -> 341,71 +337,201 -> 782,646 +786,780 -> 117,111 +977,864 -> 199,86 +544,127 -> 544,144 +539,471 -> 539,972 +364,765 -> 364,285 +282,325 -> 892,935 +536,941 -> 158,941 +280,39 -> 806,565 +168,662 -> 363,857 +639,676 -> 639,701 +397,809 -> 466,809 +716,118 -> 625,209 +153,582 -> 497,926 +35,706 -> 398,706 +61,41 -> 519,41 +158,557 -> 158,922 +320,177 -> 624,481 +800,779 -> 466,779 +292,270 -> 292,497 +919,17 -> 757,17 +935,520 -> 935,688 +948,480 -> 303,480 +306,811 -> 306,467 +227,582 -> 464,819 +757,807 -> 757,688 +983,12 -> 30,965 +632,262 -> 640,262 +755,314 -> 138,931 +179,599 -> 179,144 +556,246 -> 801,491 +114,218 -> 114,60 +183,70 -> 423,70 +561,661 -> 231,331 +875,738 -> 723,890 +812,828 -> 812,78 +98,707 -> 88,707 +804,979 -> 188,979 +503,178 -> 453,178 +902,678 -> 248,678 +603,618 -> 737,752 +788,735 -> 559,964 +839,134 -> 839,408 +569,356 -> 491,356 +566,543 -> 566,77 +624,386 -> 111,386 +781,109 -> 781,52 +187,709 -> 23,545 +220,123 -> 856,123 +210,528 -> 398,716 +942,935 -> 58,51 +102,13 -> 102,560 +926,588 -> 969,545 +529,117 -> 146,117 +989,211 -> 739,461 +207,201 -> 207,106 +799,876 -> 284,876 +370,773 -> 687,456 +571,972 -> 571,351 +877,910 -> 877,12 +384,205 -> 384,940 +493,915 -> 912,496 +764,412 -> 368,16 +368,220 -> 333,220 +526,271 -> 778,271 +379,203 -> 417,165 +168,577 -> 168,709 +529,808 -> 598,739 +959,506 -> 959,626 +576,89 -> 863,89 +277,412 -> 311,378 +12,19 -> 975,982 +620,951 -> 240,571 +937,206 -> 954,206 +231,177 -> 918,864 +499,825 -> 315,825 +289,876 -> 289,302 +408,569 -> 46,207 +461,838 -> 91,838 +281,294 -> 281,737 +61,541 -> 61,747 +768,857 -> 768,276 +782,97 -> 33,97 +544,563 -> 251,856 +731,216 -> 731,19 +767,142 -> 242,667 +469,612 -> 952,129 +508,363 -> 508,540 +614,845 -> 614,739 +342,362 -> 235,362 +880,703 -> 880,653 +942,669 -> 651,669 +884,976 -> 884,924 +158,822 -> 945,35 +510,716 -> 884,342 +523,946 -> 73,496 +334,430 -> 157,253 +593,77 -> 105,565 +269,132 -> 243,132 +902,529 -> 180,529 +554,767 -> 554,938 +164,615 -> 425,615 +51,713 -> 51,341 +428,606 -> 89,945 +600,402 -> 600,185 +774,550 -> 774,207 +465,204 -> 697,436 +577,719 -> 255,719 +647,990 -> 970,990 +649,170 -> 886,407 +428,503 -> 428,394 +381,608 -> 381,444 +778,175 -> 778,121 +795,379 -> 379,379 +929,792 -> 218,81 +504,837 -> 504,449 +212,216 -> 212,76 +446,744 -> 446,116 +824,247 -> 577,247 +77,214 -> 553,214 +913,234 -> 913,670 +949,24 -> 43,930 +733,758 -> 733,62 +628,659 -> 962,659 +172,749 -> 755,749 +901,717 -> 184,717 +457,578 -> 923,112 +943,11 -> 912,11 +728,597 -> 116,597 +465,134 -> 465,159 +170,953 -> 170,533 +231,715 -> 231,219 +209,187 -> 984,962 +798,515 -> 798,601 +479,123 -> 479,148 +360,387 -> 360,356 +962,818 -> 962,770 +852,607 -> 852,886 +159,838 -> 967,30 +823,659 -> 642,478 +374,893 -> 545,893 +248,819 -> 248,978 +894,473 -> 894,47 +182,975 -> 278,879 +75,248 -> 913,248 +969,533 -> 969,827 +18,40 -> 842,864 +972,909 -> 220,157 +378,159 -> 571,159 +875,478 -> 238,478 +95,807 -> 264,638 +418,68 -> 418,387 +784,548 -> 332,548 +365,354 -> 365,836 +731,615 -> 235,615 +885,104 -> 513,476 +816,47 -> 385,478 +626,741 -> 626,499 +371,372 -> 920,921 +83,150 -> 922,989 +623,520 -> 645,498 +612,305 -> 561,305 +845,149 -> 788,149 +914,35 -> 829,35 +143,165 -> 143,520 +164,218 -> 266,218 +118,644 -> 397,644 +59,942 -> 970,31 +616,774 -> 970,420 +30,468 -> 874,468 +454,208 -> 454,536 +524,488 -> 524,931 +54,479 -> 560,479 +815,591 -> 815,813 +959,971 -> 30,42 +23,181 -> 149,181 +841,294 -> 841,681 +34,47 -> 367,47 +913,590 -> 913,374 +690,64 -> 690,672 +541,112 -> 781,112 +380,843 -> 687,536 +303,330 -> 465,330 +408,403 -> 326,403 +352,962 -> 925,389 +121,882 -> 873,130 +979,294 -> 29,294 +228,688 -> 228,738 +845,930 -> 901,930 +726,189 -> 27,888 +223,888 -> 989,888 +483,632 -> 483,321 +606,810 -> 820,810 +225,31 -> 225,342 +841,18 -> 841,417 +375,185 -> 375,413 +641,189 -> 307,523 +126,900 -> 126,990 +530,220 -> 690,220 +496,263 -> 154,263 +140,503 -> 419,503 +349,733 -> 349,819 +43,29 -> 928,914 +683,842 -> 683,489 +113,634 -> 806,634 +771,145 -> 130,145 +88,467 -> 908,467 +328,642 -> 328,795 +986,191 -> 218,959 +857,166 -> 857,594 +950,763 -> 229,42 +263,940 -> 101,940 +689,182 -> 689,835 +241,237 -> 733,237 +965,150 -> 279,150 +871,242 -> 474,639 +688,947 -> 688,11 +319,738 -> 945,112 +21,853 -> 853,21 +69,533 -> 69,741 +492,981 -> 492,210 +942,69 -> 249,69 +63,364 -> 203,364 +340,505 -> 15,505 +41,43 -> 979,981 +395,623 -> 217,801 +540,37 -> 540,381 +64,112 -> 882,930 +887,212 -> 217,882 +168,159 -> 108,159 +117,22 -> 959,864 +413,500 -> 413,616 +775,597 -> 962,597 +171,901 -> 143,901 +777,391 -> 41,391 +901,139 -> 70,970 +215,75 -> 215,261 +973,433 -> 786,433 +757,568 -> 612,423 +363,347 -> 185,525 +274,363 -> 274,709 +435,569 -> 880,569 +267,297 -> 86,478 +221,852 -> 985,88 +322,560 -> 322,962 +470,259 -> 470,508 +861,860 -> 843,860 +172,474 -> 172,714 +53,839 -> 499,839 +600,40 -> 600,227 +820,952 -> 99,231 +650,486 -> 586,486 +305,273 -> 305,392 +826,417 -> 826,92 +309,934 -> 309,720 +381,644 -> 381,623 +38,78 -> 38,54 +326,450 -> 173,450 +474,100 -> 474,135 +607,536 -> 192,121 +686,504 -> 164,504 +538,623 -> 429,623 +200,385 -> 933,385 +568,275 -> 31,275 +105,201 -> 706,201 +582,584 -> 827,584 +24,469 -> 24,519 +306,224 -> 32,224 +429,528 -> 304,528 +272,851 -> 272,927 +636,113 -> 636,244 +481,107 -> 783,107 +834,87 -> 175,746 +684,50 -> 61,673 +30,335 -> 739,335 +621,893 -> 266,893 +968,942 -> 968,390 +895,23 -> 136,23 +742,650 -> 756,636 +42,582 -> 368,582 +890,266 -> 786,266 +591,807 -> 921,807 +915,333 -> 915,160 +746,326 -> 826,326 +663,803 -> 34,174 +533,513 -> 692,513 +205,133 -> 935,133 +730,138 -> 58,810 +290,87 -> 290,488 +693,513 -> 693,323 +188,491 -> 188,587 +562,593 -> 562,122 +629,457 -> 629,299 +132,781 -> 381,781 +356,965 -> 356,899 +720,715 -> 487,715 +356,120 -> 954,120 +657,507 -> 323,173 +13,190 -> 742,190 +677,640 -> 491,640 +145,605 -> 366,605 +143,683 -> 681,145 +700,787 -> 557,787 +958,406 -> 212,406 +267,734 -> 705,734 +470,333 -> 257,120 +790,656 -> 523,389 +13,904 -> 898,19 +29,970 -> 961,38 +846,454 -> 846,153 +564,488 -> 98,488 +904,19 -> 60,863 +493,112 -> 472,133 +945,977 -> 141,173 +720,231 -> 720,367 +783,133 -> 783,422 +165,754 -> 165,604 +752,308 -> 715,271 +413,969 -> 431,951 +833,437 -> 833,881 +612,802 -> 612,64 +974,187 -> 543,618 +655,183 -> 675,183 +696,833 -> 906,623 +756,792 -> 756,741 +338,140 -> 878,680 +854,955 -> 241,342 +602,466 -> 326,466 +470,125 -> 464,131 +568,141 -> 43,666 +826,318 -> 783,275 +194,986 -> 194,466 +896,330 -> 621,55 +482,709 -> 704,931 +345,912 -> 345,741 +758,119 -> 758,841 +11,777 -> 11,249 +88,945 -> 795,945 +665,74 -> 124,615 +243,831 -> 249,837 +40,69 -> 720,749 +757,804 -> 757,900 +803,265 -> 336,732 +299,155 -> 758,614 +787,173 -> 172,788 +251,400 -> 251,168 +217,480 -> 486,480 +939,974 -> 21,56 +767,649 -> 378,649 +197,764 -> 561,400 +767,577 -> 579,577 +952,982 -> 28,58 +282,527 -> 282,640 +944,125 -> 184,125 +149,848 -> 351,848 +36,437 -> 350,437 +63,527 -> 764,527 +66,313 -> 302,549 +805,485 -> 577,485 +660,626 -> 903,626 +927,542 -> 897,542 +577,344 -> 577,934 +624,284 -> 624,497 +649,618 -> 153,122 +942,32 -> 227,747 +10,190 -> 10,629 +84,638 -> 470,252 +362,89 -> 362,762 +351,844 -> 916,279 +683,561 -> 497,747 +628,473 -> 103,473 +319,525 -> 782,62 +842,131 -> 551,131 +980,960 -> 51,31 +662,12 -> 666,12 +337,814 -> 337,736 +720,99 -> 760,99 +867,515 -> 867,650 +248,872 -> 142,872 +295,274 -> 298,274 +102,369 -> 102,648 +523,142 -> 54,611 +369,798 -> 978,189 +215,688 -> 835,688 +846,242 -> 786,182 +68,923 -> 68,342 +690,416 -> 559,547 +567,134 -> 278,134 +89,126 -> 846,883 +779,325 -> 389,325 +675,461 -> 675,622 +278,925 -> 953,250 +907,460 -> 519,848 +769,60 -> 592,60 +331,103 -> 331,49 +148,366 -> 148,516 +933,52 -> 332,52 +488,642 -> 488,523 +632,403 -> 83,952 +321,840 -> 756,405 +302,627 -> 907,22 +650,449 -> 650,553 +219,466 -> 219,297 +841,947 -> 78,184 +40,746 -> 712,74 +559,306 -> 895,306 +317,592 -> 317,275 +267,183 -> 267,428 +43,397 -> 43,359 +952,705 -> 952,634 +921,837 -> 258,174 +634,783 -> 656,805 +366,309 -> 224,309 +383,470 -> 526,613 +717,419 -> 717,332 +543,752 -> 543,500 +954,892 -> 101,39 +294,379 -> 583,379 +829,388 -> 829,491 +748,509 -> 283,509 +347,75 -> 467,195 +618,958 -> 371,711 +986,827 -> 213,54 +34,617 -> 982,617 +716,902 -> 716,429 +970,52 -> 107,915 +563,33 -> 563,680 +803,82 -> 136,749 +24,968 -> 273,968 +816,483 -> 620,287 +783,588 -> 623,588 +397,210 -> 988,801 +911,387 -> 911,446 +770,730 -> 786,730 +22,11 -> 982,971 +395,316 -> 151,316 +301,420 -> 301,248 +10,10 -> 989,989 +637,358 -> 247,358 +932,341 -> 642,341 +162,594 -> 162,448 +51,946 -> 396,946 +591,253 -> 958,620 +567,849 -> 567,713 +879,910 -> 879,603 +889,642 -> 437,642 +669,528 -> 945,252 +644,237 -> 774,237 +488,870 -> 738,620 +692,388 -> 959,388 +506,17 -> 701,17 +663,514 -> 663,216 +684,862 -> 289,862 +511,235 -> 519,227 +866,940 -> 153,227 +381,518 -> 87,518 +837,573 -> 181,573 +337,191 -> 337,135 +324,573 -> 945,573 +449,800 -> 390,741 +763,378 -> 763,695 +24,457 -> 444,877 +267,875 -> 798,344 +724,848 -> 395,848 +931,322 -> 931,244 +426,241 -> 280,241 +175,879 -> 175,883 +496,158 -> 560,158 +899,319 -> 805,319 +799,424 -> 563,188 +958,388 -> 958,290 +558,95 -> 314,95 +768,646 -> 961,839 +246,534 -> 246,147 +808,720 -> 808,385 +912,147 -> 912,305 +670,676 -> 776,676 +534,594 -> 696,594 +736,768 -> 736,364 +377,784 -> 377,368 +799,105 -> 978,284 +763,575 -> 763,253 +581,205 -> 581,45 +932,782 -> 678,782 +400,950 -> 936,414 +68,616 -> 897,616 +399,662 -> 291,554 +354,397 -> 354,489 +219,276 -> 862,919 +115,138 -> 195,138 +863,326 -> 863,335 +884,130 -> 271,743 +986,148 -> 234,900 +254,186 -> 973,905 +975,971 -> 672,971 +122,533 -> 19,636 +316,512 -> 219,609 +113,480 -> 716,480 +483,540 -> 845,178 diff --git a/2021/05/solution.txt b/2021/05/solution.txt new file mode 100644 index 0000000..baf17f2 --- /dev/null +++ b/2021/05/solution.txt @@ -0,0 +1,2 @@ +Part 1: 5145 +Part 2: 16518 \ No newline at end of file diff --git a/2021/05/test_code.py b/2021/05/test_code.py new file mode 100644 index 0000000..4a1ac16 --- /dev/null +++ b/2021/05/test_code.py @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2021 Akumatic + +from code import part1, part2, read_file + +def test(): + vals = read_file("test_input.txt") + assert part1(vals) == 5 + print("Passed Part 1") + assert part2(vals) == 12 + print("Passed Part 2") + +if __name__ == "__main__": + test() diff --git a/2021/05/test_input.txt b/2021/05/test_input.txt new file mode 100644 index 0000000..b258f68 --- /dev/null +++ b/2021/05/test_input.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/2021/README.md b/2021/README.md index 4f3efb8..532a33d 100644 --- a/2021/README.md +++ b/2021/README.md @@ -19,7 +19,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day | 02 | :white_check_mark: | :white_check_mark: | [Solution](02/code.py) | [Day 02](https://adventofcode.com/2021/day/2) | | 03 | :white_check_mark: | :white_check_mark: | [Solution](03/code.py) | [Day 03](https://adventofcode.com/2021/day/3) | | 04 | :white_check_mark: | :white_check_mark: | [Solution](04/code.py) | [Day 04](https://adventofcode.com/2021/day/4) | -| 05 | | | | | +| 05 | :white_check_mark: | :white_check_mark: | [Solution](05/code.py) | [Day 05](https://adventofcode.com/2021/day/5) | | 06 | | | | | | 07 | | | | | | 08 | | | | |