From e4bae770de86b3b1a7fda004fa98e6bf869d7f25 Mon Sep 17 00:00:00 2001 From: Gattix Date: Sun, 12 Dec 2021 00:49:19 +0100 Subject: [PATCH] Day 5 --- 2021/day05/day05.hs | 27 +++ 2021/day05/input | 500 +++++++++++++++++++++++++++++++++++++++++++ 2021/day05/testinput | 10 + 3 files changed, 537 insertions(+) create mode 100644 2021/day05/day05.hs create mode 100644 2021/day05/input create mode 100644 2021/day05/testinput diff --git a/2021/day05/day05.hs b/2021/day05/day05.hs new file mode 100644 index 0000000..6850407 --- /dev/null +++ b/2021/day05/day05.hs @@ -0,0 +1,27 @@ +import qualified Data.Map as M +import Data.List.Split + +main :: IO () +main = do + input <- map (map read . filter (/="->")) <$> map (splitOneOf ", ") <$> lines <$> readFile "input" + let part1inp = filterOrth input + print $ length $ M.filter (> 1) $ M.fromListWith (+) (toMap part1inp) + print $ length $ M.filter (> 1) $ M.fromListWith (+) (toMap input) + +filterOrth :: [[Int]] -> [[Int]] +filterOrth [] = [] +filterOrth ([x1,y1,x2,y2]:ss) + | x1==x2 || y1==y2 = [x1,y1,x2,y2]:filterOrth ss + | otherwise = filterOrth ss + +toMap :: [[Int]] -> [((Int,Int),Int)] +toMap [] = [] +toMap ([x1,y1,x2,y2]:xs) + | y1==y2 = zip ySame (repeat 1) ++ toMap xs + | x1==x2 = zip xSame (repeat 1) ++ toMap xs + | otherwise = zip diag (repeat 1) ++ toMap xs + where diag = zip xRange yRange + ySame = zip xRange (repeat y1) + xSame = zip (repeat x1) yRange + xRange = [x1,x1-(signum $ x1-x2)..x2] + yRange = [y1,y1-(signum $ y1-y2)..y2] diff --git a/2021/day05/input b/2021/day05/input new file mode 100644 index 0000000..39de4e8 --- /dev/null +++ b/2021/day05/input @@ -0,0 +1,500 @@ +419,207 -> 419,109 +300,888 -> 803,385 +104,959 -> 457,959 +987,951 -> 987,385 +173,602 -> 919,602 +173,70 -> 305,70 +341,19 -> 486,19 +128,579 -> 128,100 +210,867 -> 969,867 +880,493 -> 880,58 +937,831 -> 131,25 +520,921 -> 476,965 +760,147 -> 461,147 +646,108 -> 646,27 +99,906 -> 99,591 +19,956 -> 19,273 +89,201 -> 326,201 +275,948 -> 962,261 +292,489 -> 689,489 +674,109 -> 20,763 +861,861 -> 529,529 +155,200 -> 273,200 +628,803 -> 209,384 +654,401 -> 578,325 +723,625 -> 828,730 +137,406 -> 862,406 +893,45 -> 41,897 +631,10 -> 941,320 +618,435 -> 537,435 +939,29 -> 30,938 +505,796 -> 505,244 +799,779 -> 77,779 +938,576 -> 427,576 +522,635 -> 405,518 +244,89 -> 946,89 +447,791 -> 316,660 +560,731 -> 687,731 +16,878 -> 835,59 +45,707 -> 45,565 +767,166 -> 404,529 +791,260 -> 791,950 +373,949 -> 373,156 +38,774 -> 38,557 +445,537 -> 445,370 +817,756 -> 959,898 +472,551 -> 952,71 +696,381 -> 657,420 +43,829 -> 43,190 +101,635 -> 728,635 +197,532 -> 140,532 +693,368 -> 299,368 +433,140 -> 433,610 +136,58 -> 136,666 +472,294 -> 886,294 +690,883 -> 671,864 +141,598 -> 141,118 +56,651 -> 56,957 +747,82 -> 747,91 +219,455 -> 55,291 +444,131 -> 444,802 +326,459 -> 528,661 +245,965 -> 143,965 +916,316 -> 630,316 +263,55 -> 977,769 +262,451 -> 587,451 +960,178 -> 960,564 +960,88 -> 476,572 +314,259 -> 314,169 +404,742 -> 429,742 +830,921 -> 409,921 +181,396 -> 463,678 +338,293 -> 23,608 +851,667 -> 851,350 +181,859 -> 718,322 +314,240 -> 870,796 +778,984 -> 77,283 +476,178 -> 440,178 +935,357 -> 841,263 +695,683 -> 414,964 +760,241 -> 306,241 +390,355 -> 791,355 +460,710 -> 851,710 +559,448 -> 870,448 +161,526 -> 301,386 +935,495 -> 633,193 +205,536 -> 383,536 +290,626 -> 290,94 +55,972 -> 946,81 +240,531 -> 631,922 +189,806 -> 573,806 +518,827 -> 866,479 +239,829 -> 260,829 +151,51 -> 849,51 +301,736 -> 532,736 +23,889 -> 336,889 +284,124 -> 284,933 +637,610 -> 67,40 +610,828 -> 610,159 +763,590 -> 763,963 +804,576 -> 804,694 +689,872 -> 82,265 +440,377 -> 190,127 +933,330 -> 310,953 +873,99 -> 873,328 +756,808 -> 860,808 +119,64 -> 928,873 +74,144 -> 489,559 +957,938 -> 838,938 +148,320 -> 932,320 +386,171 -> 386,985 +357,171 -> 494,171 +254,67 -> 254,95 +196,910 -> 827,910 +107,114 -> 758,114 +971,40 -> 801,40 +504,602 -> 215,891 +184,310 -> 720,846 +280,300 -> 955,975 +49,637 -> 49,572 +352,512 -> 739,899 +610,123 -> 585,123 +808,881 -> 758,881 +646,980 -> 818,980 +948,482 -> 384,482 +115,144 -> 852,881 +506,836 -> 547,836 +985,369 -> 374,980 +883,975 -> 48,975 +447,664 -> 312,799 +24,597 -> 24,331 +45,19 -> 979,953 +210,689 -> 210,430 +704,806 -> 704,612 +985,982 -> 124,121 +70,174 -> 550,174 +463,12 -> 637,12 +107,97 -> 716,97 +935,265 -> 390,810 +42,223 -> 42,86 +60,245 -> 60,473 +695,735 -> 208,735 +547,265 -> 802,265 +941,667 -> 941,806 +250,286 -> 611,286 +10,64 -> 630,64 +482,889 -> 482,150 +441,820 -> 776,820 +529,474 -> 529,265 +533,465 -> 217,149 +242,473 -> 242,830 +633,160 -> 476,317 +942,24 -> 942,784 +80,313 -> 92,325 +295,109 -> 295,712 +31,964 -> 857,138 +285,255 -> 955,925 +650,610 -> 650,366 +722,586 -> 625,586 +580,384 -> 580,531 +78,407 -> 896,407 +296,310 -> 730,744 +717,966 -> 924,966 +524,551 -> 524,671 +44,127 -> 784,867 +214,849 -> 238,849 +749,320 -> 749,241 +886,146 -> 336,696 +889,933 -> 455,499 +644,232 -> 79,797 +400,979 -> 626,979 +433,681 -> 433,523 +447,57 -> 676,57 +185,416 -> 659,890 +849,645 -> 257,53 +633,721 -> 633,901 +766,355 -> 766,56 +669,393 -> 669,523 +833,336 -> 833,58 +52,114 -> 52,413 +699,957 -> 109,957 +14,953 -> 945,22 +641,15 -> 929,303 +25,874 -> 866,33 +856,73 -> 28,901 +94,892 -> 592,892 +256,357 -> 256,700 +960,579 -> 31,579 +940,859 -> 940,987 +507,673 -> 820,986 +164,361 -> 133,361 +210,424 -> 876,424 +28,186 -> 28,376 +452,149 -> 531,149 +142,160 -> 142,435 +180,801 -> 180,439 +681,267 -> 42,267 +724,414 -> 786,476 +762,492 -> 762,427 +902,808 -> 227,133 +70,923 -> 821,172 +468,12 -> 457,12 +208,129 -> 986,907 +78,786 -> 78,352 +573,869 -> 820,869 +780,680 -> 520,940 +276,66 -> 276,244 +423,629 -> 592,629 +888,507 -> 888,139 +869,878 -> 869,951 +274,614 -> 625,965 +926,289 -> 982,233 +102,687 -> 102,214 +52,264 -> 52,12 +904,43 -> 657,43 +184,685 -> 184,628 +506,912 -> 601,817 +356,524 -> 87,524 +202,260 -> 202,276 +970,63 -> 83,950 +402,332 -> 950,880 +195,666 -> 843,666 +13,82 -> 892,961 +614,28 -> 614,871 +892,162 -> 892,101 +363,665 -> 59,665 +768,208 -> 410,208 +483,300 -> 295,300 +590,108 -> 881,108 +837,967 -> 837,326 +368,731 -> 368,913 +900,921 -> 873,921 +896,931 -> 848,979 +562,939 -> 857,939 +85,351 -> 598,351 +917,30 -> 624,30 +605,314 -> 605,303 +382,655 -> 340,697 +949,115 -> 653,115 +667,311 -> 370,608 +971,983 -> 39,51 +178,687 -> 178,69 +906,197 -> 296,807 +947,886 -> 383,322 +551,667 -> 551,238 +86,65 -> 916,895 +589,887 -> 865,611 +332,53 -> 84,53 +361,148 -> 55,148 +883,205 -> 661,205 +415,552 -> 52,552 +46,42 -> 46,952 +955,13 -> 39,929 +677,482 -> 208,482 +414,268 -> 927,268 +101,509 -> 101,149 +946,971 -> 139,164 +223,597 -> 223,517 +805,896 -> 796,896 +565,875 -> 878,875 +472,431 -> 472,732 +643,15 -> 643,202 +618,725 -> 618,284 +376,532 -> 376,120 +807,981 -> 415,981 +716,401 -> 61,401 +893,955 -> 743,805 +264,935 -> 264,677 +586,908 -> 638,908 +780,277 -> 780,418 +234,410 -> 428,410 +899,214 -> 899,703 +948,51 -> 948,509 +238,664 -> 879,23 +20,877 -> 638,877 +146,738 -> 109,738 +670,893 -> 524,893 +317,423 -> 27,713 +91,600 -> 477,986 +902,63 -> 902,797 +647,839 -> 647,667 +227,358 -> 236,349 +985,541 -> 660,866 +86,562 -> 86,949 +368,851 -> 863,356 +327,905 -> 57,635 +561,275 -> 781,495 +196,65 -> 626,65 +110,688 -> 720,78 +720,472 -> 115,472 +817,135 -> 817,876 +752,387 -> 752,104 +78,127 -> 635,684 +812,170 -> 155,170 +606,718 -> 804,916 +843,494 -> 979,494 +919,346 -> 454,346 +866,828 -> 818,828 +114,115 -> 114,250 +895,308 -> 370,308 +665,893 -> 690,893 +939,275 -> 741,275 +290,321 -> 290,910 +747,327 -> 107,967 +734,715 -> 391,372 +368,497 -> 506,359 +773,945 -> 391,563 +772,537 -> 733,537 +271,679 -> 488,679 +665,745 -> 665,984 +143,177 -> 685,719 +671,860 -> 147,860 +674,365 -> 857,182 +343,74 -> 985,716 +284,46 -> 180,46 +595,800 -> 20,225 +57,278 -> 792,278 +649,285 -> 165,769 +600,24 -> 600,116 +862,939 -> 862,871 +153,917 -> 682,388 +117,884 -> 257,884 +726,763 -> 531,763 +810,985 -> 899,985 +718,942 -> 718,466 +674,19 -> 674,203 +117,677 -> 117,918 +928,261 -> 928,945 +719,390 -> 719,321 +822,601 -> 484,263 +725,793 -> 725,111 +201,745 -> 588,745 +404,889 -> 908,385 +981,39 -> 610,410 +148,426 -> 711,989 +128,260 -> 319,451 +325,306 -> 325,585 +557,415 -> 557,745 +915,101 -> 648,101 +104,636 -> 104,520 +93,964 -> 641,416 +201,709 -> 201,90 +921,571 -> 798,571 +313,624 -> 313,510 +343,649 -> 28,649 +688,246 -> 24,910 +696,610 -> 353,610 +126,310 -> 126,394 +457,98 -> 457,981 +277,707 -> 277,531 +943,721 -> 37,721 +959,295 -> 702,295 +23,547 -> 891,547 +209,114 -> 931,836 +737,174 -> 737,195 +208,890 -> 115,797 +170,401 -> 726,401 +11,218 -> 11,297 +989,10 -> 10,989 +866,86 -> 487,86 +867,31 -> 867,334 +846,414 -> 861,414 +478,315 -> 478,697 +572,843 -> 731,843 +657,12 -> 161,508 +903,194 -> 142,955 +612,321 -> 147,786 +813,920 -> 259,920 +834,389 -> 651,206 +824,153 -> 824,557 +399,871 -> 115,871 +270,785 -> 270,120 +469,640 -> 753,640 +620,132 -> 620,175 +620,234 -> 666,234 +594,409 -> 948,55 +670,323 -> 670,89 +262,65 -> 262,379 +879,617 -> 284,22 +493,423 -> 761,423 +17,931 -> 906,42 +512,494 -> 473,494 +122,230 -> 122,87 +15,207 -> 533,207 +216,183 -> 50,183 +360,107 -> 280,107 +403,841 -> 941,841 +913,442 -> 500,29 +864,947 -> 864,85 +500,516 -> 634,382 +283,20 -> 669,20 +916,770 -> 176,30 +966,73 -> 252,787 +847,841 -> 171,165 +163,219 -> 766,219 +482,515 -> 275,308 +528,949 -> 240,949 +725,574 -> 847,696 +109,131 -> 109,538 +655,837 -> 476,837 +803,631 -> 803,51 +977,83 -> 149,911 +207,231 -> 171,231 +617,29 -> 617,294 +838,708 -> 446,708 +711,597 -> 612,498 +975,942 -> 279,246 +315,128 -> 315,293 +146,962 -> 873,235 +448,180 -> 54,180 +177,680 -> 866,680 +891,265 -> 741,265 +656,949 -> 414,949 +909,456 -> 196,456 +574,286 -> 58,286 +861,691 -> 861,383 +779,351 -> 779,827 +459,989 -> 459,350 +936,480 -> 936,699 +645,309 -> 348,606 +861,62 -> 621,302 +568,324 -> 568,358 +889,221 -> 889,335 +538,759 -> 538,266 +780,736 -> 780,827 +866,518 -> 983,401 +67,871 -> 840,98 +432,664 -> 664,664 +146,24 -> 755,24 +964,585 -> 964,770 +372,144 -> 809,144 +688,827 -> 867,827 +137,916 -> 137,942 +846,131 -> 846,46 +764,21 -> 457,328 +140,66 -> 799,725 +703,224 -> 83,844 +557,67 -> 557,681 +355,544 -> 764,135 +625,893 -> 126,394 +842,214 -> 842,322 +582,778 -> 582,762 +341,861 -> 341,859 +143,767 -> 52,858 +114,109 -> 114,200 +394,210 -> 396,212 +861,353 -> 861,652 +873,553 -> 62,553 +44,962 -> 984,22 +734,56 -> 734,828 +798,516 -> 950,516 +367,755 -> 367,618 +868,637 -> 868,780 +192,952 -> 192,734 +603,109 -> 705,211 +12,17 -> 984,989 +910,147 -> 910,620 +515,749 -> 515,517 +775,136 -> 761,150 +662,636 -> 662,21 +894,490 -> 310,490 +956,732 -> 297,73 +514,99 -> 140,99 +308,419 -> 691,419 +485,86 -> 485,187 +737,783 -> 979,783 +90,76 -> 869,855 +959,112 -> 84,112 +879,494 -> 879,257 +933,425 -> 933,619 +64,391 -> 64,21 +106,305 -> 253,452 +324,152 -> 853,152 +666,225 -> 39,852 +370,904 -> 257,791 +592,845 -> 592,15 +936,971 -> 267,302 +147,210 -> 62,210 +308,323 -> 495,323 +212,918 -> 110,918 +229,392 -> 685,848 +896,132 -> 326,702 +483,143 -> 605,265 +251,317 -> 130,317 +758,93 -> 445,93 +156,286 -> 458,286 +401,904 -> 383,904 +244,256 -> 851,256 +928,411 -> 612,411 +642,920 -> 642,420 +494,707 -> 494,225 +87,112 -> 87,256 +972,907 -> 83,18 +139,104 -> 139,761 +493,725 -> 493,529 +981,145 -> 459,667 +390,240 -> 702,240 +466,982 -> 807,982 +320,143 -> 692,515 +477,649 -> 477,206 +456,254 -> 456,578 diff --git a/2021/day05/testinput b/2021/day05/testinput new file mode 100644 index 0000000..b258f68 --- /dev/null +++ b/2021/day05/testinput @@ -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