From 608aa0f9d23353808005525c70652253d17f4ad1 Mon Sep 17 00:00:00 2001 From: kleph Date: Tue, 7 Dec 2021 02:30:14 +0100 Subject: [PATCH] Day 5 part 1 --- 5/5.py | 108 ++++++++++ 5/input.txt | 500 ++++++++++++++++++++++++++++++++++++++++++++ 5/input_example.txt | 10 + 3 files changed, 618 insertions(+) create mode 100755 5/5.py create mode 100644 5/input.txt create mode 100644 5/input_example.txt diff --git a/5/5.py b/5/5.py new file mode 100755 index 0000000..f676b47 --- /dev/null +++ b/5/5.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python +# 2021 - Advent Of Code - 5 + +def parse_file(file): + horizontal_lines = [] + vertical_lines = [] + with open(file) as f: + for l in f.readlines(): + p1, p2 = l.strip().split(' -> ') + strx1, stry1 = p1.split(',') + strx2, stry2 = p2.split(',') + + # filter horizontal and vertical lines only + if strx1 == strx2: + p1 = (int(strx1), int(stry1)) + p2 = (int(strx2), int(stry2)) + vertical_lines.append([p1, p2]) + if stry1 == stry2: + p1 = (int(strx1), int(stry1)) + p2 = (int(strx2), int(stry2)) + horizontal_lines.append([p1, p2]) + + return horizontal_lines, vertical_lines + + +def draw_horizontal(g, hline): + + # print(f'drawing hline: {hline}') + if hline[0][0] < hline[1][0]: # left -> right + start_point = hline[0] + end_point = hline[1] + else: + start_point = hline[1] + end_point = hline[0] + + ly = start_point[1] + # print(f'drawing from {start_point[0]} to {end_point[0]}') + for lx in range(start_point[0], end_point[0]+1): + g[ly][lx] += 1 + + +def draw_vertical(g, vline): + # print(f'drawing vline: {vline}') + if vline[0][1] < vline[1][1]: # up -> down + start_point = vline[0] + end_point = vline[1] + else: + start_point = vline[1] + end_point = vline[0] + + lx = start_point[0] + # print(f'drawing from {start_point[0]} to {end_point[0]}') + for ly in range(start_point[1], end_point[1]+1): + g[ly][lx] += 1 + + +hlines, vlines = parse_file('input.txt') +print(f'horizontal lines parsed: {len(hlines)}') +print(f'vertical lines parsed: {len(vlines)}') + +# detect size of the grid (assumed 1000x1000, but why not ;-) ) +maxx = 0 +maxy = 0 +for line in hlines + vlines: + startp = line[0] + endp = line[1] + if startp[0] > maxx: + maxx = startp[0] + if endp[0] > maxx: + maxx = endp[0] + if startp[1] > maxy: + maxy = startp[1] + if endp[1] > maxy: + maxy = endp[1] + +print(f'detected grid size: {maxx} x {maxy}') +grid = [[0] * (maxx+1) for _ in range(maxy+1)] + +# draw +for hl in hlines: + draw_horizontal(grid, hl) + +for vl in vlines: + draw_vertical(grid, vl) + +# count points +points = 0 +for y in range(maxy+1): + for x in range(maxx+1): + if grid[y][x] > 1: + points += 1 + +print(f'There\'s {points} points with at least two lines') + +# bonus (or debug with the sample), draw the grid +# print(' ', end='') +# for y in range(maxy+1): +# print(y, end='') +# print() +# +# for y in range(maxy+1): +# print(f'{y} ', end='') +# for x in range(maxx+1): +# if grid[y][x] == 0: +# print('.', end='') +# else: +# print(str(grid[y][x]), end='') +# print() diff --git a/5/input.txt b/5/input.txt new file mode 100644 index 0000000..de1bf85 --- /dev/null +++ b/5/input.txt @@ -0,0 +1,500 @@ +527,299 -> 430,299 +828,228 -> 81,228 +40,126 -> 842,928 +30,390 -> 30,741 +196,853 -> 196,599 +388,795 -> 388,300 +326,398 -> 326,766 +63,865 -> 63,407 +119,453 -> 282,453 +478,396 -> 824,396 +934,189 -> 612,189 +739,122 -> 306,555 +394,970 -> 394,249 +213,693 -> 213,336 +955,11 -> 688,11 +632,211 -> 818,211 +324,662 -> 66,404 +652,773 -> 811,614 +964,746 -> 402,184 +105,959 -> 850,214 +454,852 -> 594,852 +685,669 -> 124,669 +95,11 -> 873,789 +737,266 -> 484,266 +547,701 -> 764,484 +290,20 -> 290,164 +103,870 -> 283,870 +762,582 -> 762,431 +69,154 -> 55,154 +584,37 -> 308,37 +200,218 -> 200,336 +50,871 -> 766,155 +256,212 -> 919,875 +421,779 -> 959,241 +987,42 -> 304,725 +139,791 -> 330,600 +728,53 -> 845,53 +807,907 -> 423,907 +765,642 -> 868,642 +701,279 -> 701,75 +29,890 -> 542,890 +929,105 -> 59,975 +23,355 -> 485,817 +597,440 -> 454,440 +770,338 -> 629,338 +856,203 -> 856,858 +262,806 -> 951,806 +976,879 -> 976,179 +91,879 -> 950,20 +378,216 -> 378,440 +300,794 -> 507,794 +174,890 -> 907,157 +850,37 -> 171,37 +61,375 -> 117,375 +350,984 -> 460,984 +454,361 -> 672,361 +118,802 -> 118,448 +225,929 -> 974,180 +880,69 -> 341,608 +52,986 -> 891,147 +367,529 -> 367,707 +302,385 -> 674,385 +970,122 -> 145,947 +526,676 -> 214,364 +463,651 -> 463,708 +861,779 -> 168,86 +11,965 -> 913,63 +461,63 -> 944,63 +610,532 -> 199,121 +715,594 -> 512,594 +107,94 -> 490,94 +913,558 -> 404,558 +25,55 -> 911,941 +861,443 -> 861,329 +411,113 -> 884,113 +125,636 -> 740,636 +55,157 -> 815,917 +901,744 -> 547,744 +86,340 -> 86,866 +64,698 -> 270,698 +519,324 -> 371,324 +857,634 -> 857,35 +384,137 -> 28,137 +859,981 -> 859,302 +351,757 -> 351,204 +460,557 -> 460,792 +954,581 -> 112,581 +303,405 -> 303,599 +445,588 -> 246,588 +664,114 -> 664,107 +556,397 -> 556,714 +210,112 -> 939,841 +477,953 -> 180,656 +913,536 -> 913,333 +405,152 -> 552,152 +805,848 -> 805,12 +759,318 -> 759,163 +443,167 -> 231,167 +906,336 -> 906,122 +470,781 -> 546,705 +536,359 -> 814,81 +445,167 -> 935,167 +226,729 -> 730,729 +940,876 -> 510,446 +433,87 -> 533,87 +437,887 -> 186,887 +609,217 -> 431,217 +766,388 -> 663,388 +397,826 -> 82,826 +411,324 -> 411,437 +632,75 -> 632,290 +583,646 -> 836,899 +118,703 -> 118,307 +907,275 -> 721,275 +321,191 -> 321,362 +168,594 -> 168,55 +857,290 -> 284,290 +879,232 -> 879,456 +578,206 -> 28,756 +47,144 -> 47,777 +815,521 -> 445,151 +765,680 -> 523,438 +139,817 -> 534,422 +372,539 -> 36,539 +900,758 -> 900,987 +248,835 -> 558,835 +259,774 -> 472,774 +375,436 -> 172,436 +265,754 -> 265,556 +753,161 -> 753,307 +677,648 -> 677,743 +79,427 -> 79,34 +391,304 -> 754,304 +339,275 -> 339,548 +596,584 -> 596,684 +677,656 -> 64,43 +789,409 -> 814,434 +420,436 -> 822,436 +842,552 -> 181,552 +931,124 -> 69,986 +976,147 -> 97,147 +854,673 -> 400,219 +622,300 -> 622,339 +331,503 -> 331,585 +450,795 -> 489,756 +47,883 -> 920,10 +387,843 -> 793,843 +87,452 -> 583,948 +84,77 -> 610,603 +145,147 -> 903,147 +203,971 -> 203,67 +797,171 -> 713,171 +472,559 -> 872,959 +116,349 -> 116,43 +82,654 -> 461,275 +208,641 -> 208,25 +212,474 -> 198,488 +521,396 -> 187,396 +56,916 -> 937,916 +359,806 -> 888,806 +841,220 -> 841,857 +288,791 -> 288,610 +254,584 -> 254,37 +302,485 -> 743,926 +414,590 -> 510,686 +899,600 -> 132,600 +923,543 -> 93,543 +597,103 -> 918,103 +130,143 -> 920,933 +741,216 -> 867,216 +915,852 -> 723,852 +519,842 -> 541,842 +197,407 -> 197,526 +214,775 -> 214,728 +854,345 -> 938,345 +719,820 -> 788,751 +46,937 -> 758,225 +685,173 -> 685,354 +192,500 -> 500,500 +947,663 -> 335,51 +810,675 -> 209,74 +329,753 -> 62,753 +651,866 -> 614,866 +760,627 -> 258,627 +491,645 -> 491,473 +988,17 -> 22,983 +921,951 -> 14,44 +176,591 -> 176,750 +401,129 -> 381,129 +145,329 -> 145,905 +286,116 -> 273,103 +91,185 -> 91,756 +634,752 -> 634,14 +252,645 -> 290,645 +954,645 -> 954,350 +115,177 -> 115,121 +52,682 -> 52,51 +638,966 -> 841,763 +851,854 -> 152,155 +77,433 -> 599,955 +309,964 -> 309,426 +413,958 -> 413,235 +150,831 -> 150,118 +981,383 -> 422,942 +748,776 -> 748,631 +509,127 -> 509,231 +595,136 -> 301,136 +458,838 -> 458,912 +608,290 -> 239,290 +57,921 -> 946,32 +524,756 -> 317,963 +567,425 -> 567,443 +832,716 -> 740,716 +182,218 -> 650,686 +421,936 -> 127,642 +342,366 -> 441,267 +672,754 -> 376,754 +758,724 -> 758,379 +602,795 -> 349,795 +409,887 -> 988,308 +508,13 -> 561,66 +582,946 -> 369,733 +332,542 -> 136,346 +295,256 -> 926,256 +839,846 -> 839,698 +649,333 -> 522,333 +837,625 -> 837,184 +64,85 -> 618,639 +210,930 -> 414,930 +428,651 -> 214,651 +832,362 -> 832,926 +92,385 -> 92,799 +132,978 -> 747,978 +545,867 -> 545,947 +256,708 -> 239,725 +830,962 -> 570,962 +356,150 -> 781,575 +116,29 -> 965,878 +342,546 -> 53,835 +576,170 -> 780,170 +40,767 -> 656,151 +417,514 -> 417,114 +60,517 -> 60,137 +715,760 -> 557,760 +508,329 -> 508,371 +467,427 -> 19,427 +227,531 -> 742,16 +332,450 -> 867,450 +274,323 -> 524,323 +539,677 -> 460,677 +224,403 -> 224,845 +373,916 -> 366,916 +400,84 -> 400,895 +824,502 -> 824,804 +835,967 -> 347,479 +838,639 -> 838,798 +386,247 -> 616,17 +165,445 -> 408,445 +941,849 -> 913,849 +792,469 -> 792,600 +630,521 -> 630,344 +338,418 -> 298,458 +163,571 -> 527,571 +102,290 -> 911,290 +213,745 -> 213,267 +225,795 -> 778,242 +986,988 -> 10,12 +239,445 -> 239,829 +526,714 -> 526,225 +224,774 -> 953,45 +467,915 -> 467,291 +395,148 -> 204,339 +871,549 -> 392,549 +817,452 -> 939,330 +942,19 -> 942,937 +166,627 -> 207,627 +901,306 -> 901,158 +103,339 -> 466,339 +351,19 -> 815,19 +677,659 -> 203,185 +270,25 -> 46,25 +318,461 -> 318,129 +283,428 -> 884,428 +696,966 -> 893,966 +388,237 -> 381,230 +172,978 -> 172,18 +434,499 -> 156,777 +630,682 -> 501,682 +272,340 -> 251,361 +410,94 -> 410,885 +863,920 -> 747,920 +986,985 -> 14,13 +369,626 -> 256,513 +638,496 -> 571,496 +196,609 -> 638,609 +710,959 -> 454,959 +642,578 -> 642,468 +457,720 -> 379,798 +88,86 -> 88,320 +361,352 -> 157,556 +55,57 -> 450,57 +532,204 -> 532,750 +76,856 -> 658,274 +818,750 -> 397,329 +829,801 -> 128,100 +843,892 -> 433,892 +516,438 -> 516,59 +216,534 -> 216,914 +767,973 -> 56,973 +288,91 -> 342,91 +51,52 -> 417,418 +979,570 -> 497,570 +438,471 -> 579,330 +751,57 -> 751,619 +199,403 -> 118,403 +811,903 -> 251,343 +294,47 -> 950,703 +457,107 -> 967,617 +976,42 -> 34,984 +84,194 -> 265,194 +944,341 -> 131,341 +935,398 -> 276,398 +716,135 -> 513,135 +846,690 -> 846,950 +786,858 -> 786,960 +491,677 -> 491,57 +283,781 -> 15,513 +51,736 -> 557,230 +503,825 -> 928,825 +70,980 -> 960,90 +947,135 -> 732,350 +968,873 -> 336,241 +955,185 -> 157,983 +887,644 -> 887,530 +315,315 -> 604,604 +728,163 -> 728,67 +759,286 -> 759,55 +255,73 -> 653,471 +156,942 -> 983,115 +840,877 -> 69,106 +828,451 -> 828,117 +742,655 -> 349,655 +576,664 -> 576,427 +74,960 -> 962,72 +293,169 -> 711,169 +151,771 -> 929,771 +860,908 -> 427,475 +169,466 -> 279,356 +557,25 -> 24,25 +500,93 -> 108,93 +332,119 -> 463,119 +24,385 -> 739,385 +527,866 -> 985,866 +212,781 -> 595,398 +548,774 -> 730,774 +774,514 -> 30,514 +627,45 -> 627,885 +73,932 -> 984,21 +95,616 -> 811,616 +464,401 -> 458,401 +774,442 -> 534,202 +407,422 -> 407,211 +444,86 -> 444,358 +184,541 -> 184,325 +679,97 -> 841,259 +235,895 -> 571,895 +454,517 -> 121,517 +487,243 -> 564,243 +216,237 -> 953,974 +202,136 -> 202,536 +117,848 -> 762,203 +676,557 -> 89,557 +940,456 -> 940,548 +931,426 -> 899,426 +31,398 -> 145,398 +211,537 -> 589,159 +853,977 -> 673,977 +713,863 -> 216,366 +311,814 -> 335,814 +330,879 -> 127,879 +897,585 -> 185,585 +503,64 -> 677,64 +623,886 -> 254,517 +795,989 -> 795,516 +508,712 -> 904,316 +964,892 -> 101,29 +797,38 -> 797,901 +911,10 -> 632,10 +646,831 -> 284,831 +424,187 -> 957,720 +365,751 -> 538,578 +821,840 -> 38,57 +868,799 -> 242,799 +343,181 -> 555,393 +280,880 -> 280,795 +419,294 -> 419,79 +595,545 -> 595,775 +285,516 -> 285,816 +137,933 -> 137,61 +278,245 -> 610,577 +282,142 -> 21,142 +723,720 -> 251,248 +552,461 -> 654,461 +833,546 -> 785,546 +503,129 -> 450,76 +455,436 -> 653,436 +236,132 -> 926,822 +559,754 -> 559,615 +262,169 -> 275,169 +942,551 -> 942,273 +178,709 -> 178,69 +11,476 -> 811,476 +238,664 -> 238,867 +121,514 -> 278,514 +348,300 -> 348,453 +981,905 -> 981,139 +870,71 -> 870,69 +12,693 -> 931,693 +438,676 -> 110,348 +167,436 -> 167,839 +419,146 -> 170,146 +231,183 -> 231,278 +766,967 -> 700,967 +834,624 -> 445,624 +392,639 -> 392,808 +739,176 -> 161,176 +186,126 -> 186,451 +675,709 -> 675,629 +613,483 -> 981,483 +495,21 -> 581,107 +626,234 -> 111,234 +28,291 -> 28,883 +65,932 -> 942,55 +988,977 -> 54,43 +108,523 -> 108,820 +936,893 -> 211,168 +51,935 -> 957,29 +949,892 -> 108,51 +478,32 -> 228,32 +473,107 -> 82,498 +449,401 -> 449,645 +833,110 -> 295,648 +982,138 -> 982,111 +438,447 -> 661,224 +208,745 -> 917,36 +287,934 -> 832,389 +974,17 -> 974,105 +391,251 -> 517,377 +57,256 -> 57,704 +580,217 -> 626,171 +468,383 -> 348,383 +185,127 -> 185,851 +633,878 -> 20,265 +659,85 -> 659,698 +64,828 -> 64,592 +216,812 -> 287,812 +707,396 -> 707,167 +173,54 -> 173,723 +467,963 -> 936,963 +61,123 -> 371,433 +794,52 -> 794,595 +604,653 -> 244,293 +352,840 -> 90,840 +354,383 -> 788,383 +770,280 -> 770,621 +494,486 -> 897,486 +126,252 -> 361,252 +97,618 -> 491,224 +945,262 -> 945,72 +138,495 -> 138,107 +767,561 -> 304,561 +59,981 -> 841,199 +902,846 -> 499,846 +883,22 -> 128,22 +699,173 -> 840,32 +889,22 -> 113,798 +647,896 -> 647,226 +221,928 -> 221,66 +633,145 -> 648,130 +91,935 -> 91,162 +87,36 -> 983,932 +882,916 -> 638,916 +942,72 -> 38,976 +159,401 -> 123,437 +762,452 -> 762,721 +228,515 -> 130,613 +751,353 -> 387,717 +256,449 -> 256,811 +176,866 -> 176,778 +894,985 -> 894,462 +831,708 -> 148,25 +140,920 -> 574,920 +639,873 -> 585,873 +791,716 -> 224,149 +117,691 -> 117,158 +825,689 -> 873,641 +656,191 -> 134,191 +862,818 -> 78,34 +22,952 -> 961,13 +982,555 -> 982,21 diff --git a/5/input_example.txt b/5/input_example.txt new file mode 100644 index 0000000..b258f68 --- /dev/null +++ b/5/input_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