From 6f76176aed4b621321cbf10e508a99d0168a3308 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 5 Dec 2021 11:38:57 +0100 Subject: [PATCH] Solve day 5 part 1 --- 2021/day05_vents.py | 82 +++++++ 2021/inputs/day05 | 500 ++++++++++++++++++++++++++++++++++++++++ 2021/inputs/day05-test1 | 10 + 3 files changed, 592 insertions(+) create mode 100644 2021/day05_vents.py create mode 100644 2021/inputs/day05 create mode 100644 2021/inputs/day05-test1 diff --git a/2021/day05_vents.py b/2021/day05_vents.py new file mode 100644 index 0000000..3a2dbdf --- /dev/null +++ b/2021/day05_vents.py @@ -0,0 +1,82 @@ +from dataclasses import dataclass +from typing import List, Set + + +def main(filename: str, expected_part_1: int = None, expected_part_2: int = None): + print(f"\n+ Running on {filename}") + with open(filename) as f: + data = f.read().strip().split("\n") + + data = parse_data(data) + solution_part_1 = solve_part_1(data) + + print(f"1. Found {solution_part_1}") + if expected_part_1: + assert expected_part_1 == solution_part_1 + + solution_part_2 = solve_part_2(data) + print(f"2. Found {solution_part_2}") + if expected_part_2: + assert expected_part_2 == solution_part_2 + + +@dataclass(frozen=True) +class Point: + x: int + y: int + + +@dataclass(frozen=True) +class Segment: + start: Point + end: Point + + def is_horizontal(self): + return self.start.y == self.end.y + + def is_vertical(self): + return self.start.x == self.end.x + + def get_points(self) -> Set[Point]: + if self.is_horizontal(): + start = min(self.start.x, self.end.x) + end = max(self.start.x, self.end.x) + return {Point(x, self.start.y) for x in range(start, end + 1)} + elif self.is_vertical(): + start = min(self.start.y, self.end.y) + end = max(self.start.y, self.end.y) + return {Point(self.start.x, y) for y in range(start, end + 1)} + return set() + + +def parse_data(data: List[str]) -> List[Segment]: + segments = [] + for line in data: + start, end = line.split(" -> ") + start = start.split(",") + end = end.split(",") + start = Point(int(start[0]), int(start[1])) + end = Point(int(end[0]), int(end[1])) + segment = Segment(start, end) + segments.append(segment) + return segments + + +def solve_part_1(data: List[Segment]) -> int: + seen_points = set() + multiple_times = set() + for segment in data: + for point in segment.get_points(): + if point in seen_points: + multiple_times.add(point) + seen_points.add(point) + return len(multiple_times) + + +def solve_part_2(data: List[Segment]) -> int: + return 0 + + +if __name__ == "__main__": + main("inputs/day05-test1", expected_part_1=5) + main("inputs/day05", expected_part_1=7297) diff --git a/2021/inputs/day05 b/2021/inputs/day05 new file mode 100644 index 0000000..abe5fe3 --- /dev/null +++ b/2021/inputs/day05 @@ -0,0 +1,500 @@ +427,523 -> 427,790 +94,639 -> 94,951 +757,371 -> 465,663 +503,935 -> 503,148 +655,565 -> 655,951 +167,754 -> 710,211 +634,433 -> 245,433 +449,889 -> 449,509 +152,753 -> 207,808 +301,90 -> 301,982 +566,405 -> 482,405 +758,741 -> 847,652 +342,686 -> 921,107 +741,317 -> 741,533 +976,917 -> 976,664 +273,272 -> 273,899 +326,616 -> 326,478 +483,906 -> 391,814 +20,165 -> 20,349 +860,533 -> 860,137 +846,834 -> 846,610 +155,923 -> 241,923 +989,984 -> 17,12 +711,681 -> 82,52 +312,788 -> 312,281 +319,746 -> 892,746 +568,555 -> 757,555 +659,450 -> 940,450 +870,260 -> 870,487 +110,794 -> 604,794 +509,141 -> 509,99 +29,68 -> 29,713 +340,688 -> 820,688 +428,46 -> 902,520 +539,525 -> 539,210 +181,822 -> 806,822 +551,338 -> 551,79 +894,542 -> 894,151 +700,625 -> 700,60 +143,736 -> 143,770 +810,975 -> 54,219 +44,373 -> 44,514 +849,794 -> 120,794 +347,690 -> 97,440 +10,625 -> 57,625 +541,202 -> 799,202 +375,491 -> 809,925 +271,474 -> 271,905 +980,825 -> 980,908 +600,751 -> 624,751 +120,978 -> 958,140 +262,916 -> 262,794 +399,350 -> 399,666 +623,270 -> 158,735 +585,792 -> 585,381 +168,34 -> 168,88 +256,806 -> 566,806 +196,397 -> 326,397 +583,677 -> 172,677 +45,242 -> 311,508 +530,469 -> 433,566 +46,101 -> 877,932 +607,655 -> 485,777 +773,672 -> 461,360 +340,30 -> 659,30 +846,952 -> 846,917 +457,700 -> 125,368 +571,656 -> 561,656 +251,30 -> 576,30 +79,197 -> 79,87 +518,51 -> 518,904 +916,802 -> 181,67 +23,626 -> 485,626 +956,106 -> 956,811 +849,384 -> 743,278 +893,153 -> 893,608 +17,989 -> 933,73 +701,119 -> 701,704 +494,580 -> 494,821 +605,740 -> 605,363 +823,58 -> 823,166 +250,363 -> 566,363 +418,178 -> 783,178 +273,82 -> 273,115 +545,489 -> 656,489 +468,671 -> 966,671 +376,744 -> 178,942 +62,728 -> 589,201 +588,150 -> 467,29 +202,664 -> 751,115 +519,547 -> 916,150 +803,483 -> 380,60 +599,459 -> 922,459 +162,680 -> 162,385 +823,94 -> 18,899 +802,734 -> 415,347 +115,964 -> 115,935 +749,595 -> 749,770 +229,64 -> 582,64 +103,496 -> 551,48 +137,581 -> 707,11 +501,563 -> 895,957 +59,222 -> 620,222 +874,249 -> 874,938 +201,927 -> 927,201 +630,619 -> 655,619 +666,331 -> 413,78 +517,650 -> 425,558 +803,256 -> 803,598 +303,460 -> 606,763 +124,975 -> 967,132 +867,528 -> 867,363 +239,140 -> 239,211 +975,779 -> 364,168 +971,49 -> 531,49 +408,932 -> 143,932 +927,663 -> 392,128 +497,246 -> 497,389 +849,935 -> 15,101 +582,517 -> 543,517 +410,85 -> 237,85 +292,316 -> 841,316 +753,708 -> 753,38 +266,328 -> 267,327 +789,980 -> 16,207 +514,963 -> 514,180 +865,532 -> 687,710 +544,620 -> 956,208 +132,287 -> 557,287 +268,710 -> 268,684 +234,746 -> 234,688 +51,906 -> 51,911 +191,159 -> 428,159 +834,956 -> 834,565 +916,242 -> 230,928 +227,789 -> 227,689 +206,767 -> 705,268 +291,403 -> 578,403 +35,890 -> 956,890 +185,618 -> 402,401 +989,858 -> 284,858 +391,284 -> 391,74 +717,158 -> 670,111 +188,146 -> 785,743 +602,696 -> 602,367 +975,972 -> 975,317 +146,301 -> 146,205 +328,215 -> 861,215 +15,653 -> 990,653 +48,101 -> 48,34 +929,547 -> 506,970 +901,519 -> 670,519 +801,560 -> 19,560 +953,829 -> 876,829 +856,317 -> 856,787 +618,171 -> 86,703 +823,622 -> 369,168 +209,291 -> 846,928 +944,601 -> 944,544 +678,223 -> 987,223 +893,143 -> 54,982 +30,740 -> 244,740 +974,974 -> 15,15 +497,557 -> 63,557 +846,193 -> 846,964 +83,119 -> 946,982 +864,179 -> 864,387 +583,312 -> 77,818 +612,154 -> 612,641 +487,136 -> 487,938 +502,611 -> 215,898 +648,978 -> 648,885 +373,372 -> 318,372 +582,988 -> 168,574 +453,261 -> 899,261 +467,578 -> 33,578 +876,138 -> 683,331 +708,248 -> 132,824 +686,56 -> 686,211 +687,964 -> 687,485 +626,76 -> 626,222 +630,176 -> 630,485 +968,608 -> 968,382 +943,69 -> 943,856 +173,344 -> 173,902 +193,918 -> 326,918 +811,748 -> 379,316 +484,941 -> 113,570 +277,635 -> 156,514 +770,573 -> 691,573 +37,475 -> 464,475 +720,856 -> 676,856 +988,13 -> 25,976 +414,74 -> 140,74 +169,158 -> 29,298 +611,910 -> 611,718 +851,161 -> 507,161 +76,969 -> 76,114 +270,243 -> 976,949 +375,607 -> 325,657 +738,450 -> 196,450 +153,776 -> 153,325 +862,123 -> 862,535 +705,933 -> 931,933 +900,26 -> 35,891 +870,66 -> 79,857 +815,569 -> 347,569 +950,290 -> 757,290 +719,446 -> 317,848 +243,531 -> 671,531 +320,807 -> 380,807 +221,984 -> 221,858 +832,308 -> 832,950 +522,694 -> 584,694 +412,48 -> 412,328 +898,690 -> 646,690 +602,191 -> 106,191 +58,961 -> 58,879 +177,413 -> 147,383 +48,159 -> 48,639 +18,935 -> 883,70 +26,68 -> 406,68 +579,73 -> 579,523 +975,825 -> 975,737 +390,125 -> 737,472 +127,179 -> 127,111 +506,557 -> 576,487 +161,210 -> 324,47 +524,165 -> 524,67 +938,790 -> 184,36 +302,431 -> 548,677 +559,363 -> 559,913 +936,10 -> 176,770 +828,976 -> 657,976 +864,14 -> 864,607 +28,46 -> 28,347 +732,742 -> 732,615 +91,712 -> 91,279 +982,884 -> 772,674 +917,47 -> 683,47 +631,697 -> 83,697 +463,413 -> 463,949 +719,348 -> 197,870 +980,25 -> 24,981 +64,608 -> 410,954 +103,882 -> 103,842 +279,10 -> 830,561 +409,573 -> 409,247 +673,291 -> 222,291 +315,143 -> 942,770 +216,89 -> 80,225 +134,936 -> 967,103 +387,566 -> 82,566 +282,259 -> 862,839 +930,225 -> 930,775 +460,370 -> 460,922 +276,254 -> 518,496 +647,881 -> 293,527 +983,977 -> 90,84 +336,836 -> 336,392 +96,888 -> 963,21 +42,47 -> 978,983 +923,280 -> 726,83 +311,486 -> 505,292 +496,637 -> 861,637 +575,438 -> 575,498 +228,754 -> 228,599 +965,963 -> 21,19 +834,171 -> 730,171 +497,234 -> 497,448 +977,970 -> 977,618 +335,422 -> 335,640 +931,954 -> 36,59 +140,966 -> 871,235 +352,88 -> 449,88 +631,332 -> 735,332 +765,823 -> 48,106 +496,685 -> 103,685 +89,416 -> 89,37 +121,544 -> 569,544 +643,712 -> 596,712 +662,656 -> 739,656 +42,386 -> 42,697 +959,875 -> 959,527 +874,967 -> 120,213 +962,649 -> 240,649 +627,714 -> 266,714 +148,301 -> 148,383 +883,491 -> 883,605 +242,401 -> 348,401 +660,931 -> 420,931 +475,72 -> 955,552 +912,949 -> 912,881 +928,330 -> 45,330 +474,203 -> 131,203 +805,459 -> 818,459 +534,645 -> 639,750 +68,911 -> 933,46 +717,276 -> 21,972 +233,615 -> 957,615 +902,495 -> 902,867 +230,427 -> 230,421 +630,917 -> 845,917 +884,903 -> 34,53 +157,205 -> 157,409 +857,588 -> 608,588 +422,863 -> 422,469 +890,83 -> 722,251 +546,318 -> 138,726 +691,881 -> 52,242 +171,435 -> 209,435 +978,15 -> 702,15 +581,86 -> 581,282 +61,767 -> 61,903 +835,289 -> 835,941 +176,351 -> 707,882 +571,84 -> 571,788 +700,373 -> 700,650 +40,40 -> 945,945 +82,616 -> 809,616 +351,402 -> 455,402 +517,242 -> 73,686 +489,795 -> 814,795 +543,734 -> 611,734 +688,849 -> 688,571 +631,903 -> 883,903 +312,636 -> 667,281 +213,575 -> 699,89 +752,128 -> 161,719 +754,931 -> 754,474 +748,666 -> 748,54 +123,667 -> 57,667 +140,497 -> 140,644 +634,961 -> 187,514 +822,865 -> 822,126 +58,162 -> 632,736 +417,164 -> 166,164 +943,597 -> 943,274 +355,756 -> 355,167 +340,546 -> 232,438 +166,125 -> 166,96 +354,394 -> 354,579 +147,844 -> 155,844 +652,723 -> 719,656 +584,582 -> 667,665 +514,373 -> 685,202 +308,404 -> 966,404 +124,179 -> 916,971 +706,597 -> 455,597 +52,822 -> 510,364 +419,900 -> 860,900 +153,649 -> 872,649 +915,160 -> 56,160 +422,346 -> 422,38 +940,73 -> 940,784 +922,581 -> 922,753 +949,821 -> 949,621 +591,778 -> 145,778 +93,685 -> 164,756 +287,469 -> 287,948 +953,952 -> 446,445 +935,47 -> 12,970 +458,109 -> 458,639 +71,643 -> 644,70 +812,214 -> 812,655 +488,496 -> 158,496 +113,868 -> 755,226 +666,429 -> 666,920 +962,246 -> 517,246 +400,261 -> 400,713 +601,617 -> 943,275 +904,847 -> 120,63 +556,334 -> 278,612 +416,463 -> 59,463 +724,401 -> 871,548 +701,210 -> 426,485 +786,116 -> 786,904 +976,979 -> 157,160 +674,377 -> 674,891 +321,829 -> 891,829 +404,84 -> 404,800 +975,850 -> 906,781 +753,331 -> 978,331 +87,240 -> 87,383 +167,390 -> 167,20 +258,931 -> 107,780 +672,313 -> 696,313 +589,88 -> 589,13 +637,65 -> 637,808 +532,610 -> 532,756 +612,482 -> 318,482 +522,322 -> 135,322 +89,49 -> 89,679 +475,506 -> 475,973 +385,348 -> 229,348 +633,978 -> 633,255 +978,833 -> 978,436 +746,553 -> 707,553 +970,820 -> 970,548 +589,59 -> 589,19 +674,890 -> 16,890 +838,315 -> 413,740 +39,129 -> 860,950 +923,755 -> 425,257 +757,929 -> 940,746 +639,390 -> 104,925 +650,512 -> 260,902 +718,90 -> 987,90 +352,228 -> 795,671 +141,629 -> 491,279 +755,616 -> 368,616 +117,113 -> 707,703 +206,73 -> 926,793 +372,415 -> 372,130 +894,632 -> 611,632 +974,554 -> 878,554 +823,710 -> 823,972 +730,711 -> 582,859 +94,912 -> 108,912 +782,190 -> 88,884 +668,567 -> 336,567 +87,266 -> 327,506 +738,303 -> 36,303 +320,358 -> 445,358 +566,725 -> 695,725 +94,364 -> 94,64 +884,184 -> 159,909 +947,575 -> 947,662 +658,741 -> 658,470 +512,776 -> 512,960 +96,757 -> 950,757 +349,604 -> 349,760 +459,247 -> 355,143 +90,12 -> 95,12 +28,27 -> 981,980 +388,959 -> 508,959 +704,194 -> 148,194 +349,546 -> 349,259 +30,38 -> 720,728 +307,631 -> 307,282 +520,417 -> 154,417 +746,771 -> 924,771 +838,932 -> 20,114 +406,141 -> 406,75 +15,35 -> 963,983 +264,143 -> 948,827 +545,899 -> 742,899 +759,356 -> 309,356 +686,707 -> 224,245 +226,985 -> 962,249 +826,628 -> 826,637 +985,329 -> 683,631 +586,283 -> 586,258 +602,703 -> 243,703 +419,883 -> 419,556 +475,375 -> 211,111 +981,15 -> 19,977 +989,10 -> 10,989 +238,350 -> 479,591 +720,714 -> 603,714 +969,985 -> 35,51 +844,356 -> 837,349 +450,354 -> 450,834 +385,870 -> 385,758 +911,856 -> 89,34 +948,124 -> 989,124 +439,556 -> 439,310 +990,233 -> 990,633 +747,723 -> 943,527 +418,23 -> 804,23 +876,96 -> 38,934 +556,92 -> 532,92 +727,155 -> 727,279 +674,545 -> 940,811 +118,879 -> 420,879 +141,50 -> 426,335 +591,180 -> 591,537 +662,511 -> 681,511 +776,858 -> 776,956 +872,329 -> 87,329 +183,985 -> 114,985 +186,179 -> 186,801 +354,933 -> 491,933 +10,223 -> 759,972 +112,626 -> 702,36 +498,78 -> 161,78 +196,61 -> 814,679 +629,71 -> 116,71 +23,281 -> 23,32 +819,810 -> 432,423 +187,276 -> 187,912 +95,683 -> 576,683 +125,813 -> 856,82 +98,882 -> 140,882 +843,909 -> 198,909 +192,904 -> 192,335 +375,342 -> 375,668 +61,804 -> 61,832 +475,387 -> 966,878 +822,740 -> 822,186 diff --git a/2021/inputs/day05-test1 b/2021/inputs/day05-test1 new file mode 100644 index 0000000..1d4e36d --- /dev/null +++ b/2021/inputs/day05-test1 @@ -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