From 78438fa736da027a3fe48399e4b84dc43a1dec9c Mon Sep 17 00:00:00 2001 From: opfez Date: Sun, 5 Dec 2021 11:23:57 +0100 Subject: [PATCH] day 5 --- day5/first.hs | 45 +++++ day5/input | 500 +++++++++++++++++++++++++++++++++++++++++++++++++ day5/second.hs | 48 +++++ 3 files changed, 593 insertions(+) create mode 100644 day5/first.hs create mode 100644 day5/input create mode 100644 day5/second.hs diff --git a/day5/first.hs b/day5/first.hs new file mode 100644 index 0000000..851d6ab --- /dev/null +++ b/day5/first.hs @@ -0,0 +1,45 @@ +{-# LANGUAGE RecordWildCards #-} +import Data.List (group, sort) + +wordsWhen :: (a -> Bool) -> [a] -> [[a]] +wordsWhen pred s = + case dropWhile pred s of + [] -> [] + s' -> w : wordsWhen pred s'' + where (w, s'') = break pred s' + +type Point = (Int, Int) + +data Line = Line + { x1 :: Int + , y1 :: Int + , x2 :: Int + , y2 :: Int + } deriving (Show, Eq) + +toLine :: [Int] -> Line +toLine (x1:y1:x2:y2:[]) = Line x1 y1 x2 y2 + +parseLines = filter (\l -> x1 l == x2 l || y1 l == y2 l) + . map toLine + . map (map (read :: String -> Int)) + . map (filter (/="->")) + . map (wordsWhen (\c -> c == ',' || c == ' ')) + . lines + +pointsOfLine :: Line -> [Point] +pointsOfLine Line{..} + | x1 == x2 = [(x1, y) | y <- [min y1 y2 .. max y1 y2]] + | y1 == y2 = [(x, y1) | x <- [min x1 x2 .. max x1 x2]] + | otherwise = error "???" + +solution :: [Line] -> Int +solution = length + . filter (>1) + . map length + . group + . sort + . concat + . map pointsOfLine + +main = interact $ show . solution . parseLines diff --git a/day5/input b/day5/input new file mode 100644 index 0000000..c6631c5 --- /dev/null +++ b/day5/input @@ -0,0 +1,500 @@ +35,968 -> 974,29 +198,552 -> 124,478 +731,697 -> 828,697 +335,693 -> 335,699 +936,853 -> 936,798 +120,93 -> 798,93 +150,334 -> 150,876 +482,383 -> 388,383 +983,262 -> 202,262 +324,185 -> 473,334 +403,500 -> 205,500 +785,273 -> 200,273 +415,48 -> 860,48 +18,472 -> 125,365 +198,878 -> 378,878 +764,136 -> 764,421 +295,476 -> 500,271 +109,828 -> 109,675 +219,302 -> 746,302 +122,964 -> 122,977 +191,96 -> 191,898 +88,338 -> 88,679 +966,116 -> 150,932 +524,872 -> 423,771 +504,373 -> 213,82 +95,42 -> 552,499 +33,135 -> 474,135 +581,697 -> 938,340 +450,138 -> 59,138 +225,295 -> 746,295 +68,27 -> 922,881 +868,926 -> 868,928 +403,257 -> 961,815 +758,981 -> 289,512 +22,968 -> 954,36 +329,424 -> 200,295 +123,783 -> 158,783 +263,191 -> 263,813 +11,946 -> 830,127 +46,292 -> 830,292 +323,977 -> 21,675 +536,469 -> 186,469 +831,16 -> 292,555 +409,476 -> 409,209 +500,870 -> 802,870 +424,30 -> 424,599 +956,275 -> 956,732 +832,589 -> 353,589 +963,648 -> 843,528 +848,262 -> 887,262 +726,874 -> 29,874 +38,80 -> 599,641 +808,210 -> 670,348 +347,19 -> 727,19 +679,811 -> 663,811 +316,619 -> 540,843 +360,631 -> 331,631 +844,968 -> 577,701 +64,622 -> 64,200 +974,660 -> 332,18 +795,11 -> 930,11 +733,606 -> 733,17 +878,519 -> 878,331 +832,209 -> 603,209 +577,572 -> 261,256 +958,318 -> 479,797 +723,177 -> 352,548 +364,735 -> 364,143 +26,982 -> 980,28 +32,407 -> 276,407 +192,312 -> 212,292 +218,937 -> 916,239 +314,185 -> 436,185 +489,689 -> 691,689 +964,280 -> 964,260 +936,410 -> 175,410 +900,182 -> 900,317 +537,866 -> 537,351 +584,330 -> 584,587 +906,234 -> 632,234 +274,986 -> 274,646 +633,901 -> 430,901 +973,66 -> 615,66 +221,703 -> 221,868 +562,658 -> 920,658 +200,561 -> 200,277 +265,517 -> 628,517 +907,341 -> 907,199 +942,16 -> 329,16 +112,867 -> 947,32 +858,464 -> 431,37 +754,84 -> 754,976 +954,629 -> 954,702 +983,65 -> 90,958 +522,904 -> 753,673 +710,726 -> 129,726 +175,48 -> 787,48 +428,575 -> 808,195 +283,814 -> 283,256 +725,630 -> 725,590 +984,242 -> 984,494 +712,236 -> 712,818 +186,802 -> 378,802 +142,934 -> 984,934 +144,661 -> 355,661 +10,138 -> 834,962 +812,707 -> 812,426 +710,419 -> 710,367 +577,398 -> 77,398 +380,915 -> 380,120 +653,799 -> 442,799 +12,444 -> 879,444 +821,138 -> 529,430 +20,269 -> 528,777 +774,404 -> 832,404 +547,241 -> 368,420 +744,701 -> 362,701 +807,730 -> 60,730 +58,954 -> 58,925 +926,367 -> 677,367 +443,894 -> 443,865 +975,258 -> 975,285 +506,296 -> 703,296 +654,94 -> 76,94 +451,700 -> 297,546 +284,237 -> 840,237 +763,724 -> 150,111 +47,819 -> 47,69 +935,53 -> 818,53 +824,369 -> 547,369 +297,326 -> 297,179 +535,973 -> 628,973 +780,834 -> 245,834 +132,548 -> 988,548 +634,606 -> 949,606 +385,87 -> 385,92 +107,553 -> 107,468 +537,275 -> 537,413 +100,18 -> 57,61 +360,512 -> 360,584 +363,339 -> 22,339 +666,785 -> 661,785 +570,323 -> 570,838 +639,38 -> 399,38 +647,950 -> 218,521 +104,768 -> 104,968 +975,261 -> 44,261 +898,460 -> 391,460 +212,883 -> 212,65 +430,886 -> 430,818 +157,568 -> 157,685 +659,111 -> 712,111 +498,298 -> 59,298 +750,548 -> 135,548 +383,742 -> 383,17 +776,525 -> 776,623 +143,930 -> 143,291 +101,559 -> 774,559 +473,99 -> 368,204 +564,898 -> 343,677 +381,935 -> 27,581 +814,618 -> 814,811 +760,581 -> 760,876 +961,238 -> 240,959 +383,133 -> 383,503 +691,442 -> 616,442 +490,240 -> 236,494 +327,724 -> 769,724 +60,251 -> 986,251 +416,680 -> 580,844 +691,825 -> 944,825 +148,72 -> 148,276 +574,482 -> 574,313 +296,327 -> 296,391 +226,521 -> 226,396 +757,741 -> 932,741 +332,678 -> 226,678 +687,147 -> 576,147 +730,405 -> 730,564 +629,354 -> 629,39 +989,988 -> 11,10 +208,393 -> 208,608 +516,301 -> 305,512 +946,633 -> 946,552 +820,16 -> 820,337 +717,136 -> 717,117 +761,951 -> 761,117 +34,797 -> 773,58 +11,41 -> 947,977 +328,561 -> 464,561 +279,400 -> 913,400 +203,952 -> 964,191 +829,673 -> 474,673 +144,898 -> 461,581 +423,892 -> 203,892 +501,865 -> 97,865 +640,111 -> 640,108 +297,57 -> 624,384 +656,441 -> 656,757 +930,980 -> 930,974 +605,579 -> 827,579 +149,178 -> 149,886 +694,797 -> 694,133 +261,219 -> 261,263 +823,810 -> 32,19 +74,168 -> 718,812 +667,501 -> 873,501 +726,681 -> 223,681 +708,78 -> 140,78 +19,804 -> 749,804 +508,900 -> 508,957 +553,545 -> 245,237 +469,86 -> 702,86 +157,92 -> 157,693 +480,834 -> 824,834 +249,639 -> 471,639 +863,852 -> 337,852 +865,886 -> 256,277 +257,225 -> 310,225 +114,632 -> 114,446 +735,859 -> 36,859 +117,357 -> 117,108 +952,976 -> 185,976 +885,539 -> 885,970 +547,86 -> 974,513 +69,763 -> 69,21 +790,857 -> 282,349 +81,953 -> 972,62 +135,138 -> 135,112 +30,449 -> 310,169 +954,11 -> 18,947 +945,60 -> 20,985 +59,341 -> 544,341 +393,648 -> 955,648 +622,600 -> 312,290 +279,272 -> 279,876 +14,870 -> 874,10 +798,514 -> 798,487 +948,961 -> 11,24 +80,333 -> 237,490 +23,218 -> 23,117 +895,199 -> 980,199 +800,116 -> 615,116 +602,740 -> 759,740 +989,397 -> 780,188 +666,164 -> 666,798 +418,447 -> 507,536 +513,665 -> 649,665 +78,56 -> 602,580 +849,780 -> 849,588 +158,900 -> 941,117 +586,283 -> 586,660 +718,954 -> 422,954 +774,191 -> 139,826 +207,733 -> 207,284 +974,19 -> 14,979 +729,41 -> 729,97 +28,971 -> 943,56 +166,44 -> 882,760 +533,255 -> 65,255 +175,228 -> 175,966 +420,245 -> 420,498 +885,111 -> 117,879 +112,140 -> 925,953 +777,426 -> 925,278 +22,596 -> 22,952 +718,570 -> 87,570 +196,204 -> 68,204 +372,456 -> 372,494 +825,876 -> 156,207 +568,25 -> 286,25 +839,950 -> 247,950 +435,76 -> 421,76 +746,550 -> 294,550 +900,178 -> 900,453 +228,209 -> 900,209 +762,363 -> 613,363 +156,298 -> 156,528 +267,536 -> 267,436 +433,300 -> 954,300 +596,586 -> 596,659 +147,390 -> 845,390 +803,89 -> 295,597 +841,57 -> 101,57 +74,36 -> 696,658 +546,452 -> 919,825 +330,575 -> 862,43 +575,689 -> 868,982 +356,649 -> 356,600 +893,27 -> 168,752 +248,479 -> 547,479 +804,631 -> 804,192 +230,903 -> 230,454 +501,675 -> 487,675 +943,61 -> 117,887 +43,866 -> 43,278 +83,90 -> 723,730 +103,824 -> 103,143 +496,775 -> 496,768 +638,237 -> 638,112 +614,632 -> 184,632 +476,810 -> 358,810 +174,909 -> 715,368 +676,555 -> 16,555 +34,928 -> 34,854 +187,90 -> 110,167 +467,438 -> 372,533 +218,726 -> 326,726 +927,58 -> 468,517 +754,746 -> 754,900 +613,705 -> 613,712 +423,440 -> 423,422 +291,657 -> 680,268 +286,989 -> 286,479 +959,720 -> 959,648 +577,571 -> 355,571 +208,145 -> 982,919 +957,19 -> 957,363 +576,253 -> 576,192 +528,262 -> 22,262 +945,352 -> 914,352 +978,746 -> 737,746 +613,279 -> 17,875 +586,370 -> 768,370 +462,403 -> 462,451 +322,526 -> 322,973 +873,104 -> 75,902 +314,505 -> 689,130 +817,657 -> 813,657 +812,88 -> 775,51 +639,91 -> 639,489 +541,430 -> 541,346 +318,176 -> 702,176 +375,821 -> 279,821 +982,975 -> 21,14 +958,965 -> 54,61 +307,124 -> 539,124 +236,879 -> 581,879 +109,942 -> 688,942 +36,863 -> 786,113 +54,739 -> 54,837 +710,131 -> 27,131 +945,45 -> 10,980 +859,295 -> 859,610 +853,916 -> 853,44 +123,421 -> 711,421 +605,817 -> 666,817 +328,934 -> 308,954 +566,644 -> 798,644 +229,861 -> 217,861 +155,263 -> 155,830 +369,813 -> 369,337 +857,61 -> 517,61 +111,597 -> 382,326 +279,478 -> 899,478 +937,709 -> 288,60 +686,258 -> 979,258 +775,511 -> 907,511 +594,518 -> 105,29 +524,443 -> 171,443 +250,861 -> 373,861 +508,645 -> 952,645 +473,299 -> 767,299 +339,637 -> 734,242 +602,488 -> 941,149 +577,514 -> 577,494 +30,107 -> 207,107 +973,911 -> 973,277 +212,629 -> 674,167 +37,887 -> 37,517 +135,783 -> 322,783 +747,851 -> 216,320 +794,317 -> 794,756 +67,908 -> 162,908 +476,278 -> 809,278 +327,503 -> 327,751 +906,76 -> 58,924 +509,962 -> 43,496 +122,678 -> 582,218 +683,969 -> 431,969 +804,740 -> 804,932 +680,192 -> 259,192 +502,928 -> 195,621 +164,68 -> 978,882 +432,738 -> 430,740 +947,809 -> 947,937 +383,502 -> 383,933 +966,577 -> 929,577 +571,793 -> 486,793 +990,143 -> 803,143 +509,781 -> 509,563 +80,803 -> 80,347 +542,612 -> 143,612 +218,787 -> 218,100 +724,842 -> 476,594 +168,19 -> 885,19 +163,134 -> 163,617 +313,706 -> 462,855 +289,250 -> 867,828 +639,677 -> 854,677 +590,245 -> 16,819 +567,158 -> 567,868 +205,813 -> 89,813 +944,132 -> 125,951 +365,726 -> 917,174 +978,978 -> 15,15 +124,368 -> 54,368 +61,905 -> 826,140 +279,618 -> 616,281 +554,412 -> 125,841 +28,173 -> 712,857 +155,971 -> 803,323 +204,214 -> 748,214 +778,473 -> 778,239 +58,219 -> 792,219 +898,70 -> 64,904 +834,675 -> 658,675 +114,870 -> 114,226 +845,618 -> 845,194 +541,137 -> 323,355 +51,849 -> 164,849 +223,70 -> 223,855 +295,30 -> 295,191 +763,316 -> 67,316 +793,460 -> 186,460 +10,149 -> 834,973 +722,972 -> 183,433 +693,681 -> 974,681 +398,486 -> 846,38 +534,774 -> 609,774 +69,899 -> 606,899 +903,682 -> 493,682 +490,380 -> 490,203 +845,877 -> 845,464 +93,349 -> 617,349 +810,526 -> 810,422 +178,374 -> 178,161 +49,377 -> 49,693 +566,542 -> 79,542 +866,438 -> 866,518 +966,762 -> 540,336 +949,276 -> 190,276 +161,326 -> 827,326 +962,576 -> 750,576 +218,217 -> 218,411 +606,879 -> 606,470 +855,791 -> 571,507 +425,301 -> 937,301 +920,174 -> 125,969 +498,943 -> 498,982 +744,717 -> 744,976 +660,134 -> 834,134 +942,481 -> 942,134 +759,461 -> 759,556 +255,690 -> 255,984 +436,754 -> 967,223 +659,429 -> 695,429 +256,166 -> 256,506 +91,405 -> 804,405 +211,869 -> 245,903 +986,886 -> 986,216 +337,603 -> 337,988 +388,631 -> 230,473 +86,324 -> 448,686 +626,168 -> 626,702 +542,849 -> 681,988 +824,454 -> 667,611 +962,352 -> 962,705 +463,168 -> 463,985 +226,808 -> 467,808 +555,649 -> 56,649 +804,75 -> 74,805 +583,793 -> 583,274 +933,106 -> 123,916 +563,842 -> 294,573 +50,601 -> 518,601 +140,669 -> 100,669 +930,542 -> 930,393 +954,650 -> 262,650 +56,295 -> 708,947 +43,565 -> 303,305 +183,470 -> 34,470 +151,519 -> 83,519 +727,594 -> 727,129 +561,620 -> 561,311 +985,94 -> 985,473 +11,63 -> 934,986 +293,924 -> 162,924 +741,780 -> 549,972 +65,561 -> 65,722 +903,447 -> 542,808 +13,350 -> 13,564 +838,661 -> 838,248 +94,434 -> 65,434 +870,839 -> 870,933 +982,528 -> 982,262 +816,693 -> 816,521 +685,645 -> 448,645 +707,579 -> 163,35 +330,313 -> 151,313 diff --git a/day5/second.hs b/day5/second.hs new file mode 100644 index 0000000..2654240 --- /dev/null +++ b/day5/second.hs @@ -0,0 +1,48 @@ +{-# LANGUAGE RecordWildCards #-} +import Data.List (group, sort) + +wordsWhen :: (a -> Bool) -> [a] -> [[a]] +wordsWhen pred s = + case dropWhile pred s of + [] -> [] + s' -> w : wordsWhen pred s'' + where (w, s'') = break pred s' + +type Point = (Int, Int) + +data Line = Line + { x1 :: Int + , y1 :: Int + , x2 :: Int + , y2 :: Int + } deriving (Show, Eq) + +toLine :: [Int] -> Line +toLine (x1:y1:x2:y2:[]) = Line x1 y1 x2 y2 + +parseLines :: String -> [Line] +parseLines = map toLine + . map (map (read :: String -> Int)) + . map (filter (/="->")) + . map (wordsWhen (\c -> c == ',' || c == ' ')) + . lines + +pointsOfLine :: Line -> [Point] +pointsOfLine Line{..} + | x1 == x2 = [(x1, y) | y <- [min y1 y2 .. max y1 y2]] + | y1 == y2 = [(x, y1) | x <- [min x1 x2 .. max x1 x2]] + | otherwise = [(x1 + (fst off) * dx, y1 + (snd off) * dy) + | off <- zip [0..abs $ x1 - x2] [0..abs $ y1 - y2]] + where dx = if x1 < x2 then 1 else -1 + dy = if y1 < y2 then 1 else -1 + +solution :: [Line] -> Int +solution = length + . filter (>1) + . map length + . group + . sort + . concat + . map pointsOfLine + +main = interact $ show . solution . parseLines