From 372f7ec0a7e8c3358129f200f5b5b74e79ae83ff Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sun, 5 Dec 2021 23:05:13 +0100 Subject: Day 5 part 1 --- day5/__init__.py | 74 ++++++++ day5/example.txt | 10 ++ day5/input.txt | 500 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ day5/test_init.py | 15 ++ 4 files changed, 599 insertions(+) create mode 100644 day5/__init__.py create mode 100644 day5/example.txt create mode 100644 day5/input.txt create mode 100644 day5/test_init.py (limited to 'day5') diff --git a/day5/__init__.py b/day5/__init__.py new file mode 100644 index 0000000..4d5260d --- /dev/null +++ b/day5/__init__.py @@ -0,0 +1,74 @@ +from collections import Counter +from copy import copy +from dataclasses import dataclass +from typing import Iterator, List, Optional, Tuple, Callable + +from aoc import BaseAssignment + +Vector = Tuple[Tuple[int, int], Tuple[int, int]] + +class Assignment(BaseAssignment): + def parse_item(self, item: str) -> Vector: + start, end = item.split('->') + return ( + tuple(int(_) for _ in start.strip().split(',')), + tuple(int(_) for _ in end.strip().split(',')), + ) + + def read_input(self, example = False) -> List[Vector]: + return list(super().read_input(example)) + + +def is_horizontal(v: Vector): + return v[0][0] == v[1][0] + +def is_vertical(v: Vector): + return v[0][1] == v[1][1] + +def points_in_vector(vector: Vector) -> List[Tuple[int, int]]: + first_x = min(vector[0][0], vector[1][0]) + last_x = max(vector[0][0], vector[1][0]) + first_y = min(vector[0][1], vector[1][1]) + last_y = max(vector[0][1], vector[1][1]) + + return [ + (vector[0][0], y) + for y + in range(first_y, last_y + 1) + ] if is_horizontal(vector) else [ + (x, vector[0][1]) + for x + in range(first_x, last_x + 1) + ] if is_vertical(vector) else [] + + +class AssignmentOne(Assignment): + example_result = 5 + + def run(self, input: List[Vector]) -> int: + coordinates = [] + + for vector in input: + coordinates += points_in_vector(vector) + + return len([c for c in Counter(coordinates).values() if c >= 2]) + + + + +# class AssignmentTwo(Assignment): +# example_result = 1924 +# +# def run(self, input: Tuple[List[int], List[BingoCard]]) -> int: +# nrs, cards = input +# +# in_game = copy(cards) +# for nr in nrs: +# for card in copy(in_game): +# card.mark(nr) +# +# if card.bingo: +# in_game.remove(card) +# +# if len(in_game) == 0: +# return nr * sum(card.unmarked) \ No newline at end of file diff --git a/day5/example.txt b/day5/example.txt new file mode 100644 index 0000000..1d4e36d --- /dev/null +++ b/day5/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 \ No newline at end of file diff --git a/day5/input.txt b/day5/input.txt new file mode 100644 index 0000000..fc4f01d --- /dev/null +++ b/day5/input.txt @@ -0,0 +1,500 @@ +498,436 -> 498,932 +173,176 -> 845,848 +927,799 -> 927,418 +576,67 -> 801,67 +908,147 -> 743,147 +300,478 -> 300,224 +286,979 -> 286,310 +230,435 -> 729,934 +260,602 -> 260,56 +82,686 -> 655,113 +460,918 -> 460,224 +191,820 -> 454,820 +964,483 -> 964,772 +395,705 -> 142,705 +281,563 -> 134,563 +155,509 -> 152,509 +754,586 -> 742,586 +620,114 -> 620,775 +450,476 -> 450,212 +135,845 -> 591,845 +922,902 -> 110,90 +562,71 -> 895,71 +843,13 -> 843,620 +413,398 -> 486,471 +206,601 -> 409,398 +854,813 -> 65,813 +625,17 -> 293,349 +778,807 -> 778,145 +372,655 -> 566,655 +520,490 -> 520,289 +450,787 -> 450,717 +416,649 -> 826,649 +479,601 -> 313,601 +817,560 -> 817,862 +135,272 -> 287,272 +705,250 -> 796,159 +457,656 -> 445,656 +667,829 -> 659,829 +804,450 -> 804,410 +565,322 -> 565,113 +645,614 -> 972,614 +94,634 -> 421,634 +739,903 -> 122,903 +730,549 -> 963,782 +798,563 -> 969,563 +784,245 -> 176,245 +439,872 -> 448,872 +107,586 -> 906,586 +33,162 -> 573,702 +48,426 -> 362,112 +927,662 -> 927,939 +723,363 -> 723,389 +321,237 -> 982,237 +554,541 -> 554,726 +779,292 -> 779,61 +864,491 -> 864,970 +497,482 -> 497,801 +919,767 -> 919,965 +57,611 -> 57,221 +200,481 -> 200,229 +164,292 -> 164,608 +103,664 -> 694,73 +950,162 -> 181,931 +977,336 -> 293,336 +946,912 -> 80,912 +805,142 -> 89,858 +976,85 -> 245,85 +987,961 -> 987,530 +783,683 -> 443,683 +276,746 -> 490,532 +424,352 -> 424,725 +103,31 -> 559,31 +697,284 -> 697,305 +292,789 -> 627,454 +61,83 -> 151,83 +686,199 -> 652,233 +780,883 -> 780,512 +901,609 -> 346,54 +136,939 -> 136,612 +678,594 -> 408,594 +987,693 -> 987,178 +783,517 -> 828,517 +718,846 -> 23,151 +416,286 -> 518,286 +480,612 -> 130,612 +801,805 -> 801,259 +676,749 -> 119,192 +330,954 -> 936,348 +258,407 -> 258,791 +497,804 -> 668,804 +330,723 -> 497,556 +691,253 -> 691,408 +141,68 -> 365,68 +643,497 -> 305,159 +498,938 -> 765,671 +982,825 -> 580,825 +863,300 -> 959,396 +291,64 -> 291,745 +601,609 -> 601,182 +564,428 -> 564,66 +792,365 -> 792,161 +718,123 -> 718,813 +545,493 -> 903,851 +176,13 -> 988,825 +514,205 -> 514,415 +86,825 -> 446,825 +684,867 -> 684,951 +265,917 -> 936,246 +310,655 -> 310,920 +794,370 -> 809,370 +170,475 -> 75,570 +128,644 -> 200,572 +293,105 -> 600,105 +846,984 -> 976,984 +531,311 -> 977,311 +749,565 -> 315,565 +892,311 -> 452,751 +29,980 -> 974,35 +433,355 -> 433,307 +718,528 -> 261,985 +649,488 -> 156,488 +571,265 -> 119,717 +801,31 -> 66,766 +984,521 -> 213,521 +553,973 -> 20,973 +981,984 -> 15,18 +174,117 -> 174,880 +308,161 -> 308,443 +342,633 -> 342,507 +871,822 -> 258,822 +899,835 -> 83,19 +557,242 -> 557,211 +163,57 -> 895,789 +252,84 -> 342,174 +863,512 -> 863,66 +351,458 -> 351,304 +548,427 -> 266,145 +794,624 -> 794,303 +408,889 -> 408,457 +10,782 -> 10,216 +837,230 -> 837,213 +973,809 -> 109,809 +913,53 -> 41,925 +637,324 -> 921,40 +806,603 -> 806,800 +814,181 -> 41,954 +320,790 -> 574,790 +281,683 -> 646,683 +145,344 -> 246,344 +806,128 -> 806,164 +677,855 -> 650,882 +177,841 -> 177,25 +775,254 -> 41,254 +337,685 -> 563,459 +69,271 -> 69,463 +748,390 -> 504,634 +370,203 -> 370,338 +401,914 -> 436,879 +562,139 -> 266,435 +253,99 -> 232,99 +238,270 -> 142,270 +532,555 -> 433,555 +219,23 -> 919,723 +988,783 -> 988,795 +60,663 -> 12,663 +392,961 -> 962,961 +116,805 -> 23,712 +739,824 -> 68,824 +968,461 -> 245,461 +511,926 -> 918,519 +76,729 -> 367,438 +335,138 -> 335,849 +888,53 -> 86,855 +660,111 -> 660,534 +938,981 -> 63,106 +190,487 -> 145,487 +13,738 -> 13,18 +964,959 -> 16,11 +940,508 -> 456,508 +311,516 -> 311,391 +249,430 -> 597,778 +944,60 -> 944,337 +696,228 -> 490,228 +189,458 -> 341,610 +414,970 -> 570,970 +51,912 -> 960,912 +736,923 -> 96,923 +884,482 -> 552,150 +819,41 -> 168,41 +762,806 -> 169,806 +285,59 -> 890,59 +891,751 -> 469,751 +147,466 -> 147,868 +151,626 -> 151,45 +986,986 -> 10,10 +539,681 -> 539,979 +530,273 -> 530,181 +613,140 -> 599,154 +426,473 -> 426,467 +338,795 -> 857,795 +548,711 -> 715,711 +479,30 -> 479,177 +16,871 -> 75,812 +410,621 -> 552,621 +730,814 -> 58,814 +990,728 -> 209,728 +164,783 -> 164,820 +523,689 -> 189,355 +86,531 -> 875,531 +470,585 -> 152,267 +704,484 -> 551,484 +829,197 -> 776,197 +39,10 -> 979,950 +653,144 -> 844,144 +80,462 -> 104,486 +249,703 -> 160,703 +817,725 -> 817,15 +972,895 -> 173,96 +610,28 -> 610,664 +452,581 -> 452,848 +243,623 -> 144,623 +837,175 -> 757,95 +592,843 -> 592,210 +233,505 -> 848,505 +591,710 -> 820,481 +945,46 -> 247,744 +357,883 -> 961,279 +615,900 -> 185,900 +93,673 -> 976,673 +629,692 -> 298,361 +888,111 -> 888,652 +873,876 -> 366,369 +14,373 -> 14,370 +690,786 -> 73,169 +305,888 -> 305,811 +429,490 -> 429,442 +177,92 -> 177,836 +892,733 -> 754,733 +442,211 -> 442,896 +686,21 -> 686,809 +413,689 -> 275,689 +522,601 -> 522,525 +41,952 -> 925,68 +635,97 -> 538,194 +849,826 -> 351,826 +915,254 -> 877,292 +118,64 -> 928,64 +36,21 -> 985,970 +334,374 -> 589,374 +152,742 -> 286,742 +714,441 -> 459,696 +871,370 -> 871,191 +410,527 -> 410,62 +713,114 -> 804,205 +577,271 -> 577,369 +144,72 -> 939,867 +424,139 -> 424,491 +458,585 -> 228,815 +550,151 -> 932,533 +179,200 -> 179,981 +423,524 -> 701,246 +755,552 -> 755,689 +728,250 -> 840,362 +892,583 -> 892,785 +328,241 -> 506,241 +837,441 -> 837,414 +101,44 -> 942,885 +917,833 -> 451,833 +52,216 -> 35,199 +964,375 -> 451,888 +572,509 -> 572,680 +602,558 -> 602,396 +555,652 -> 763,652 +198,617 -> 523,617 +585,964 -> 585,721 +376,534 -> 556,534 +360,466 -> 360,757 +875,571 -> 875,68 +964,757 -> 595,757 +787,785 -> 804,785 +670,863 -> 343,863 +449,453 -> 449,681 +16,313 -> 657,954 +958,68 -> 706,320 +726,635 -> 726,715 +790,865 -> 790,459 +926,946 -> 13,33 +61,761 -> 247,761 +386,939 -> 386,426 +909,192 -> 909,216 +387,161 -> 28,520 +182,740 -> 182,178 +443,975 -> 715,703 +194,714 -> 870,38 +636,522 -> 636,308 +153,220 -> 248,315 +532,427 -> 72,887 +673,521 -> 464,521 +191,362 -> 749,920 +718,747 -> 306,335 +85,733 -> 561,733 +710,714 -> 243,714 +191,132 -> 704,645 +983,60 -> 76,60 +740,708 -> 460,988 +105,987 -> 908,184 +629,943 -> 629,136 +386,515 -> 236,365 +988,10 -> 12,986 +989,479 -> 989,855 +232,860 -> 232,612 +719,526 -> 406,213 +548,242 -> 175,242 +172,21 -> 172,295 +158,935 -> 158,267 +743,331 -> 109,965 +177,465 -> 177,883 +694,46 -> 694,714 +589,328 -> 915,328 +721,865 -> 170,314 +56,373 -> 934,373 +558,635 -> 558,199 +187,116 -> 323,252 +743,875 -> 827,791 +313,219 -> 718,624 +418,175 -> 830,175 +620,244 -> 557,244 +33,199 -> 33,768 +595,562 -> 595,75 +725,294 -> 725,97 +645,554 -> 552,554 +909,828 -> 849,828 +820,25 -> 110,25 +591,803 -> 961,803 +101,109 -> 760,109 +473,593 -> 473,609 +990,714 -> 822,714 +263,183 -> 263,301 +919,409 -> 414,914 +554,36 -> 554,165 +223,168 -> 989,934 +764,30 -> 725,30 +518,730 -> 185,397 +482,548 -> 482,472 +933,34 -> 484,34 +210,494 -> 210,769 +354,659 -> 278,659 +79,172 -> 603,696 +703,120 -> 300,523 +466,228 -> 315,77 +977,43 -> 515,505 +474,95 -> 163,95 +213,275 -> 213,39 +166,12 -> 332,12 +931,606 -> 122,606 +117,424 -> 616,424 +493,980 -> 21,508 +172,492 -> 766,492 +630,59 -> 301,59 +377,10 -> 970,603 +119,287 -> 119,332 +635,829 -> 635,313 +626,132 -> 550,208 +520,570 -> 27,77 +817,974 -> 347,974 +943,99 -> 943,734 +470,895 -> 686,679 +92,303 -> 286,497 +869,714 -> 869,879 +63,234 -> 777,948 +426,461 -> 622,461 +832,275 -> 832,410 +898,937 -> 248,287 +797,891 -> 70,164 +642,974 -> 642,143 +954,654 -> 868,654 +603,34 -> 871,302 +42,849 -> 65,849 +683,377 -> 534,526 +390,227 -> 490,227 +422,980 -> 422,223 +761,640 -> 612,640 +238,170 -> 959,891 +663,674 -> 904,433 +675,833 -> 165,833 +535,128 -> 535,14 +509,444 -> 509,519 +137,668 -> 442,668 +74,427 -> 74,685 +291,827 -> 291,486 +557,302 -> 196,302 +960,164 -> 210,914 +599,418 -> 392,418 +655,339 -> 13,981 +202,243 -> 847,888 +13,329 -> 359,675 +814,191 -> 814,802 +555,942 -> 555,377 +879,359 -> 879,935 +622,841 -> 622,821 +639,592 -> 823,592 +699,187 -> 46,840 +967,927 -> 286,927 +484,185 -> 484,441 +103,442 -> 232,442 +176,19 -> 459,302 +696,871 -> 109,871 +424,475 -> 424,722 +90,28 -> 837,28 +177,114 -> 737,674 +626,753 -> 736,753 +133,616 -> 133,180 +934,62 -> 934,442 +100,883 -> 100,497 +549,483 -> 729,483 +976,39 -> 36,979 +386,140 -> 114,412 +363,459 -> 219,459 +563,77 -> 563,598 +142,60 -> 827,745 +460,519 -> 610,519 +56,249 -> 325,249 +924,492 -> 768,336 +165,726 -> 475,416 +57,418 -> 580,418 +756,554 -> 163,554 +82,237 -> 846,237 +123,99 -> 383,99 +816,527 -> 816,65 +224,171 -> 866,813 +833,92 -> 362,92 +671,808 -> 671,984 +522,25 -> 522,43 +248,424 -> 16,192 +297,225 -> 805,225 +454,534 -> 79,534 +380,936 -> 184,936 +286,866 -> 286,97 +543,807 -> 224,807 +518,700 -> 600,782 +961,809 -> 566,414 +140,298 -> 140,695 +328,51 -> 328,534 +927,942 -> 28,43 +166,654 -> 745,75 +25,976 -> 988,13 +563,99 -> 721,99 +605,808 -> 605,648 +483,603 -> 836,956 +39,127 -> 39,815 +942,157 -> 409,690 +358,593 -> 733,218 +655,297 -> 892,297 +477,276 -> 919,276 +355,583 -> 825,113 +186,329 -> 522,665 +497,838 -> 725,838 +868,54 -> 200,54 +270,14 -> 270,307 +47,33 -> 47,218 +238,190 -> 280,232 +857,700 -> 56,700 +593,164 -> 38,719 +765,237 -> 25,977 +517,48 -> 517,570 +584,587 -> 311,587 +781,268 -> 641,128 +564,656 -> 564,154 +31,530 -> 31,496 +50,36 -> 954,940 +17,989 -> 987,19 +280,88 -> 762,88 +104,698 -> 261,541 +68,303 -> 388,303 +959,966 -> 356,966 +794,91 -> 35,850 +783,197 -> 608,197 +413,506 -> 218,311 +642,10 -> 118,534 +812,963 -> 657,963 +877,828 -> 850,828 +214,546 -> 214,116 +559,685 -> 700,826 +494,66 -> 774,66 +886,730 -> 886,660 +811,67 -> 820,67 +375,510 -> 678,510 +711,868 -> 490,647 +420,945 -> 420,222 +453,948 -> 279,948 +956,749 -> 956,311 +55,846 -> 55,260 +402,94 -> 799,94 +683,206 -> 683,324 +710,269 -> 187,269 +120,114 -> 120,784 +401,616 -> 401,989 +641,650 -> 850,650 +424,208 -> 351,135 +31,985 -> 988,28 \ No newline at end of file diff --git a/day5/test_init.py b/day5/test_init.py new file mode 100644 index 0000000..5096a60 --- /dev/null +++ b/day5/test_init.py @@ -0,0 +1,15 @@ +from day5 import points_in_vector + + +def test_points_in_vector(): + assert points_in_vector(((1,1), (1,3))) == [ + (1, 1), (1, 2), (1, 3) + ] + + assert points_in_vector(((9,7), (7,7))) == [ + (7, 7), (8, 7), (9, 7) + ] + + + + -- cgit v1.2.3