From 698b3c5282a7dc2ea331fb0f55a8795a14124b2c Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 5 Dec 2021 11:00:13 +0000 Subject: [PATCH] Done day 5, brute force version --- advent-of-code21.cabal | 5 + advent05/Main.hs | 119 ++++++++++ data/advent05.txt | 500 +++++++++++++++++++++++++++++++++++++++++ problems/day05.html | 175 +++++++++++++++ 4 files changed, 799 insertions(+) create mode 100644 advent05/Main.hs create mode 100644 data/advent05.txt create mode 100644 problems/day05.html diff --git a/advent-of-code21.cabal b/advent-of-code21.cabal index 9819761..69ff677 100644 --- a/advent-of-code21.cabal +++ b/advent-of-code21.cabal @@ -99,3 +99,8 @@ executable advent04 import: common-extensions, build-directives main-is: advent04/Main.hs build-depends: text, attoparsec + +executable advent05 + import: common-extensions, build-directives + main-is: advent05/Main.hs + build-depends: text, attoparsec, linear, containers diff --git a/advent05/Main.hs b/advent05/Main.hs new file mode 100644 index 0000000..2171ef7 --- /dev/null +++ b/advent05/Main.hs @@ -0,0 +1,119 @@ +-- Writeup at https://work.njae.me.uk/2021/12/04/advent-of-code-2021-day-4/ + +import Data.Text () +import qualified Data.Text.IO as TIO + +import Data.Attoparsec.Text +import Control.Applicative + +import qualified Data.Map.Strict as M +import Linear (V2(..), (^+^)) + + +type Point = V2 Int +type Grid = M.Map Point Int + +data Line = Line Point Point deriving (Eq, Show) + + +main :: IO () +main = + do text <- TIO.readFile "data/advent05.txt" + let trace = successfulParse text + print $ part1 trace + print $ part2 trace + +part1 trace = M.size $ M.filter (> 1) diagramHV + where hLines = map expandLineH $ filter isHoriz trace + vLines = map expandLineV $ filter isVert trace + diagramH = addLines M.empty hLines + diagramHV = addLines diagramH vLines + +part2 trace = M.size $ M.filter (> 1) diagram4 + where hLines = map expandLineH $ filter isHoriz trace + vLines = map expandLineV $ filter isVert trace + pdLines = map expandLinePD $ filter isPDiag trace + ndLines = map expandLineND $ filter isNDiag trace + diagram1 = addLines M.empty hLines + diagram2 = addLines diagram1 vLines + diagram3 = addLines diagram2 pdLines + diagram4 = addLines diagram3 ndLines + + + +isHoriz :: Line -> Bool +isHoriz (Line (V2 x0 _y0) (V2 x1 _y1)) = x0 == x1 + +isVert :: Line -> Bool +isVert (Line (V2 _x0 y0) (V2 _x1 y1)) = y0 == y1 + +-- x and y increasing together +isPDiag :: Line -> Bool +isPDiag (Line (V2 x0 y0) (V2 x1 y1)) = + (x0 /= x1) + && (y0 /= y1) + && ((x1 - x0) == (y1 - y0)) + +-- x increasing, y decreasing +isNDiag :: Line -> Bool +isNDiag (Line (V2 x0 y0) (V2 x1 y1)) = + (x0 /= x1) + && (y0 /= y1) + && ((x1 - x0) == (y0 - y1)) + + +-- horizOrVert :: Line -> Bool +-- horizOrVert line = isHoriz line || isVert line + + +-- hovLines = filter horizOrVert + +expandLineH :: Line -> [Point] +expandLineH (Line (V2 x0 y0) (V2 _x1 y1)) = -- x0 == x1 + [V2 x0 y | y <- [yMin..yMax]] + where yMin = min y0 y1 + yMax = max y0 y1 + +expandLineV :: Line -> [Point] +expandLineV (Line (V2 x0 y0) (V2 x1 _y1)) = -- y0 == y1 + [V2 x y0 | x <- [xMin..xMax]] + where xMin = min x0 x1 + xMax = max x0 x1 + +-- x and y increasing together +expandLinePD :: Line -> [Point] +expandLinePD (Line (V2 x0 y0) (V2 x1 y1)) = map (uncurry V2) coords + where xMin = min x0 x1 + xMax = max x0 x1 + yMin = min y0 y1 + yMax = max y0 y1 + coords = zip [xMin..xMax] [yMin..yMax] + +-- x increasing, y decreasing +expandLineND :: Line -> [Point] +expandLineND (Line (V2 x0 y0) (V2 x1 y1)) = map (uncurry V2) coords + where xMin = min x0 x1 + xMax = max x0 x1 + yMin = min y0 y1 + yMax = max y0 y1 + yMax1 = yMax - 1 + coords = zip [xMin..xMax] [yMax, yMax1..yMin] + + +addLines :: Grid -> [[Point]] -> Grid +addLines diagram expandedLines = foldr insertLine diagram expandedLines + where insertPoint p d = M.insertWith (+) p 1 d + insertLine l d = foldr insertPoint d l + + +-- Parse the input file + +traceP = lineP `sepBy` endOfLine +lineP = Line <$> (pointP <* " -> ") <*> pointP +pointP = V2 <$> (decimal <* ",") <*> decimal + +-- successfulParse :: Text -> (Integer, [Maybe Integer]) +successfulParse input = + case parseOnly traceP input of + Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right trace -> trace diff --git a/data/advent05.txt b/data/advent05.txt new file mode 100644 index 0000000..1dc889e --- /dev/null +++ b/data/advent05.txt @@ -0,0 +1,500 @@ +260,605 -> 260,124 +308,411 -> 656,63 +570,791 -> 396,617 +622,593 -> 622,240 +944,801 -> 829,916 +310,439 -> 310,676 +448,595 -> 867,176 +293,167 -> 975,849 +375,252 -> 830,707 +679,819 -> 498,819 +58,391 -> 58,653 +674,489 -> 745,489 +296,172 -> 646,522 +63,976 -> 944,976 +471,377 -> 471,17 +172,980 -> 172,136 +942,61 -> 29,974 +238,304 -> 44,498 +961,196 -> 199,196 +219,452 -> 219,87 +40,709 -> 40,132 +657,239 -> 657,860 +95,579 -> 95,522 +740,613 -> 572,445 +71,243 -> 71,861 +313,561 -> 121,753 +775,350 -> 775,271 +872,864 -> 872,942 +677,879 -> 685,879 +456,425 -> 456,227 +719,845 -> 719,245 +543,467 -> 543,446 +254,921 -> 523,921 +792,794 -> 391,393 +247,839 -> 911,839 +645,381 -> 645,39 +297,239 -> 297,358 +106,582 -> 263,739 +269,907 -> 600,907 +41,405 -> 276,405 +765,454 -> 765,570 +587,243 -> 587,193 +945,972 -> 14,41 +896,597 -> 896,338 +564,952 -> 564,83 +533,79 -> 15,597 +367,972 -> 367,243 +280,135 -> 180,135 +140,26 -> 140,757 +646,781 -> 13,148 +245,29 -> 245,436 +482,565 -> 482,327 +608,709 -> 608,687 +379,392 -> 379,98 +400,839 -> 400,484 +531,735 -> 274,735 +771,946 -> 771,850 +724,626 -> 724,983 +359,726 -> 426,726 +665,610 -> 665,342 +119,708 -> 294,708 +398,393 -> 816,811 +810,133 -> 986,133 +640,35 -> 97,578 +376,682 -> 673,682 +394,363 -> 946,363 +755,869 -> 755,898 +252,178 -> 557,178 +798,687 -> 239,687 +171,913 -> 907,177 +432,540 -> 432,751 +127,863 -> 127,117 +618,513 -> 618,348 +908,961 -> 44,97 +944,200 -> 276,868 +388,244 -> 602,30 +686,433 -> 131,988 +413,867 -> 858,422 +873,65 -> 201,737 +66,65 -> 219,65 +354,792 -> 199,637 +198,19 -> 977,798 +372,321 -> 213,321 +938,169 -> 845,76 +798,679 -> 267,679 +833,350 -> 833,275 +764,838 -> 505,838 +590,771 -> 76,257 +89,664 -> 636,117 +955,284 -> 34,284 +152,850 -> 152,816 +772,504 -> 772,969 +527,242 -> 82,242 +699,493 -> 462,256 +592,751 -> 462,881 +61,427 -> 395,427 +144,711 -> 144,611 +483,760 -> 717,760 +616,268 -> 616,82 +542,947 -> 542,44 +396,830 -> 501,725 +589,520 -> 241,868 +397,631 -> 397,533 +87,928 -> 892,928 +789,17 -> 222,584 +639,192 -> 229,192 +617,482 -> 407,692 +197,13 -> 197,153 +786,715 -> 786,42 +634,203 -> 784,203 +370,562 -> 517,415 +726,188 -> 713,188 +859,974 -> 222,337 +584,48 -> 584,519 +28,493 -> 173,638 +252,697 -> 252,503 +810,380 -> 706,380 +367,805 -> 676,496 +266,321 -> 266,527 +327,635 -> 240,548 +108,460 -> 283,460 +309,314 -> 309,599 +653,300 -> 653,13 +327,283 -> 327,596 +597,102 -> 597,374 +646,630 -> 484,792 +215,643 -> 215,202 +227,437 -> 64,437 +768,774 -> 768,587 +703,698 -> 274,698 +849,401 -> 841,401 +235,122 -> 551,122 +446,102 -> 340,208 +749,317 -> 107,959 +94,628 -> 110,612 +25,611 -> 620,16 +989,227 -> 30,227 +894,987 -> 840,987 +24,967 -> 979,12 +659,890 -> 659,571 +255,28 -> 228,28 +598,500 -> 598,447 +804,700 -> 921,700 +45,574 -> 45,154 +112,242 -> 112,402 +77,684 -> 77,691 +975,454 -> 960,469 +24,977 -> 985,16 +924,387 -> 924,177 +815,73 -> 815,656 +687,277 -> 687,100 +273,404 -> 144,533 +511,560 -> 687,560 +248,507 -> 38,507 +671,896 -> 671,866 +750,83 -> 244,589 +408,73 -> 946,611 +627,455 -> 627,299 +48,982 -> 48,795 +371,880 -> 454,963 +766,794 -> 275,303 +199,106 -> 24,281 +461,649 -> 489,649 +966,935 -> 192,161 +675,309 -> 952,32 +389,110 -> 389,119 +182,111 -> 275,111 +10,887 -> 832,65 +643,871 -> 643,488 +282,170 -> 282,65 +11,43 -> 925,957 +19,509 -> 492,36 +80,906 -> 668,318 +582,163 -> 582,153 +556,401 -> 556,116 +853,348 -> 185,348 +989,987 -> 14,12 +209,742 -> 924,27 +970,545 -> 970,364 +195,905 -> 848,252 +86,96 -> 24,96 +142,69 -> 484,69 +984,919 -> 107,42 +406,291 -> 613,291 +859,306 -> 520,306 +563,343 -> 754,343 +434,345 -> 558,345 +498,82 -> 800,384 +641,412 -> 952,412 +614,873 -> 614,706 +956,19 -> 11,964 +486,618 -> 575,707 +32,114 -> 736,818 +699,854 -> 343,854 +645,777 -> 581,777 +607,302 -> 254,302 +495,316 -> 495,587 +47,391 -> 302,391 +901,320 -> 548,320 +112,144 -> 112,218 +780,904 -> 780,582 +814,761 -> 279,761 +15,622 -> 456,622 +986,987 -> 10,11 +13,988 -> 985,16 +413,802 -> 640,802 +118,634 -> 118,771 +34,235 -> 34,976 +154,941 -> 154,195 +115,184 -> 763,832 +381,568 -> 191,568 +460,565 -> 628,565 +217,67 -> 775,67 +375,446 -> 375,585 +593,408 -> 448,408 +564,410 -> 564,567 +921,805 -> 540,424 +794,447 -> 794,485 +892,19 -> 892,618 +811,789 -> 811,923 +36,16 -> 988,968 +431,454 -> 293,592 +598,611 -> 430,779 +760,767 -> 29,767 +347,124 -> 501,124 +851,954 -> 55,158 +698,573 -> 439,573 +78,687 -> 647,118 +181,644 -> 928,644 +393,424 -> 89,424 +210,245 -> 712,747 +654,74 -> 406,322 +29,287 -> 98,287 +307,337 -> 144,337 +696,156 -> 111,156 +86,424 -> 86,299 +148,875 -> 900,123 +918,768 -> 359,209 +826,278 -> 294,810 +572,697 -> 939,697 +984,709 -> 132,709 +311,314 -> 815,314 +713,342 -> 424,53 +220,570 -> 737,570 +335,222 -> 335,344 +483,454 -> 978,949 +32,925 -> 865,92 +936,668 -> 565,668 +520,45 -> 520,931 +920,486 -> 910,476 +787,901 -> 787,450 +613,271 -> 355,13 +859,415 -> 834,415 +144,28 -> 144,850 +121,971 -> 249,971 +458,786 -> 967,786 +282,213 -> 282,587 +925,940 -> 371,940 +100,927 -> 927,927 +979,984 -> 25,30 +583,532 -> 961,910 +986,82 -> 986,436 +382,583 -> 641,842 +973,131 -> 973,697 +558,908 -> 567,917 +807,423 -> 315,423 +49,240 -> 328,240 +723,535 -> 831,535 +714,922 -> 714,765 +303,789 -> 303,972 +85,466 -> 85,391 +306,725 -> 306,322 +398,592 -> 342,592 +689,902 -> 95,308 +421,731 -> 421,396 +12,687 -> 12,902 +412,522 -> 412,158 +535,100 -> 981,546 +198,711 -> 198,91 +287,196 -> 719,628 +350,144 -> 931,144 +497,800 -> 497,278 +68,919 -> 68,876 +199,519 -> 199,284 +796,321 -> 245,321 +827,640 -> 254,67 +674,842 -> 473,842 +11,227 -> 203,227 +295,462 -> 10,747 +869,574 -> 583,288 +427,761 -> 303,885 +593,760 -> 373,760 +415,238 -> 415,641 +694,556 -> 721,556 +127,672 -> 716,83 +698,769 -> 156,227 +325,198 -> 826,198 +64,23 -> 986,945 +338,942 -> 338,597 +41,835 -> 852,835 +497,332 -> 569,404 +166,734 -> 747,153 +623,820 -> 623,637 +49,238 -> 921,238 +958,829 -> 174,45 +485,732 -> 79,732 +413,765 -> 537,889 +340,204 -> 340,158 +986,434 -> 710,434 +104,90 -> 915,90 +332,112 -> 332,760 +731,692 -> 490,933 +258,913 -> 311,913 +271,890 -> 717,890 +451,898 -> 425,872 +722,564 -> 768,564 +316,630 -> 316,60 +712,220 -> 712,658 +532,202 -> 387,57 +794,231 -> 44,981 +587,409 -> 587,535 +907,195 -> 907,760 +233,776 -> 963,46 +267,665 -> 267,148 +303,183 -> 952,183 +908,469 -> 908,725 +518,976 -> 753,976 +845,636 -> 531,636 +774,225 -> 51,948 +914,797 -> 632,797 +853,343 -> 542,654 +174,637 -> 567,637 +193,220 -> 622,220 +90,229 -> 14,229 +284,745 -> 82,543 +795,174 -> 795,210 +524,507 -> 265,507 +371,953 -> 371,844 +906,402 -> 123,402 +192,881 -> 192,549 +519,299 -> 726,92 +986,989 -> 986,706 +774,55 -> 493,55 +702,290 -> 165,290 +200,650 -> 683,167 +367,603 -> 321,603 +193,31 -> 704,542 +630,885 -> 703,885 +330,966 -> 330,285 +960,306 -> 960,820 +281,62 -> 281,25 +12,542 -> 12,627 +669,505 -> 237,937 +237,174 -> 200,211 +66,424 -> 585,943 +92,115 -> 881,904 +559,282 -> 920,282 +67,815 -> 67,47 +199,985 -> 140,926 +70,25 -> 979,934 +694,555 -> 27,555 +622,644 -> 159,644 +582,983 -> 256,983 +230,712 -> 315,712 +976,877 -> 190,91 +680,957 -> 680,245 +257,767 -> 257,315 +943,380 -> 943,412 +320,95 -> 320,774 +843,644 -> 292,93 +416,342 -> 416,605 +302,682 -> 302,925 +79,107 -> 885,913 +18,980 -> 973,25 +379,947 -> 379,963 +11,987 -> 987,11 +634,749 -> 634,310 +875,295 -> 718,295 +818,904 -> 471,557 +963,803 -> 963,133 +640,870 -> 939,571 +54,423 -> 547,916 +880,121 -> 880,332 +671,275 -> 31,275 +820,559 -> 820,609 +45,924 -> 936,33 +233,314 -> 136,217 +680,634 -> 680,883 +780,217 -> 531,217 +477,304 -> 477,166 +904,725 -> 904,472 +307,326 -> 850,869 +875,435 -> 341,969 +714,382 -> 544,552 +981,934 -> 197,934 +621,439 -> 621,786 +414,902 -> 414,238 +821,546 -> 243,546 +313,753 -> 301,753 +510,566 -> 510,593 +38,601 -> 210,429 +344,411 -> 344,490 +653,970 -> 653,698 +32,155 -> 746,155 +814,701 -> 26,701 +928,141 -> 430,141 +890,804 -> 972,804 +374,183 -> 374,387 +821,327 -> 907,413 +180,27 -> 180,813 +848,194 -> 848,496 +905,589 -> 618,302 +633,114 -> 659,114 +546,613 -> 217,942 +191,336 -> 344,336 +265,442 -> 834,442 +801,388 -> 801,339 +399,212 -> 731,212 +649,559 -> 649,112 +783,108 -> 178,108 +113,670 -> 948,670 +524,596 -> 257,329 +371,397 -> 371,784 +344,465 -> 344,819 +307,234 -> 307,453 +984,964 -> 31,11 +466,743 -> 841,743 +185,722 -> 745,722 +845,36 -> 147,734 +77,21 -> 982,926 +415,252 -> 69,252 +420,976 -> 420,526 +104,694 -> 104,645 +806,197 -> 29,974 +278,101 -> 494,101 +598,664 -> 17,83 +175,708 -> 175,764 +888,392 -> 270,392 +141,564 -> 141,672 +507,255 -> 956,255 +519,36 -> 89,36 +662,526 -> 661,526 +642,970 -> 642,725 +684,79 -> 237,526 +352,166 -> 583,166 +335,366 -> 400,366 +568,627 -> 789,406 +115,257 -> 115,925 +803,110 -> 803,643 +694,189 -> 114,769 +441,344 -> 217,568 +207,838 -> 207,163 +832,975 -> 832,384 +340,178 -> 340,729 +251,550 -> 268,533 +324,962 -> 396,962 +649,875 -> 649,692 +87,939 -> 984,42 +947,966 -> 34,53 +368,555 -> 777,964 +24,31 -> 977,984 +168,471 -> 168,740 +689,717 -> 689,256 +836,78 -> 105,809 +538,542 -> 963,117 +247,779 -> 247,331 +198,968 -> 717,968 +85,61 -> 85,444 +410,439 -> 410,482 +678,940 -> 29,291 +893,907 -> 270,284 +355,493 -> 355,439 +89,902 -> 809,902 +73,507 -> 73,180 +686,405 -> 740,351 +939,372 -> 354,957 +774,388 -> 774,402 +424,720 -> 542,720 +495,570 -> 246,570 +583,904 -> 583,680 +816,458 -> 275,458 +206,654 -> 633,654 +70,94 -> 70,919 +826,538 -> 821,538 +837,451 -> 370,918 +421,667 -> 902,186 +415,863 -> 942,863 +13,364 -> 13,958 +181,687 -> 150,718 +265,698 -> 931,32 +10,17 -> 967,974 +507,323 -> 507,324 +429,21 -> 429,533 +843,351 -> 843,812 +380,148 -> 631,399 +313,762 -> 198,647 +814,183 -> 493,183 +923,456 -> 874,407 +857,100 -> 10,947 +893,115 -> 25,983 \ No newline at end of file diff --git a/problems/day05.html b/problems/day05.html new file mode 100644 index 0000000..73dd960 --- /dev/null +++ b/problems/day05.html @@ -0,0 +1,175 @@ + + + + +Day 5 - Advent of Code 2021 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 10*

      /*2021*/

+ + + +
+ +

--- Day 5: Hydrothermal Venture ---

You come across a field of hydrothermal vents on the ocean floor! These vents constantly produce large, opaque clouds, so it would be best to avoid them if possible.

+

They tend to form in lines; the submarine helpfully produces a list of nearby lines of vents (your puzzle input) for you to review. For example:

+
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
+
+

Each line of vents is given as a line segment in the format x1,y1 -> x2,y2 where x1,y1 are the coordinates of one end the line segment and x2,y2 are the coordinates of the other end. These line segments include the points at both ends. In other words:

+
    +
  • An entry like 1,1 -> 1,3 covers points 1,1, 1,2, and 1,3.
  • +
  • An entry like 9,7 -> 7,7 covers points 9,7, 8,7, and 7,7.
  • +
+

For now, only consider horizontal and vertical lines: lines where either x1 = x2 or y1 = y2.

+

So, the horizontal and vertical lines from the above list would produce the following diagram:

+
.......1..
+..1....1..
+..1....1..
+.......1..
+.112111211
+..........
+..........
+..........
+..........
+222111....
+
+

In this diagram, the top left corner is 0,0 and the bottom right corner is 9,9. Each position is shown as the number of lines which cover that point or . if no line covers that point. The top-left pair of 1s, for example, comes from 2,2 -> 2,1; the very bottom row is formed by the overlapping lines 0,9 -> 5,9 and 0,9 -> 2,9.

+

To avoid the most dangerous areas, you need to determine the number of points where at least two lines overlap. In the above example, this is anywhere in the diagram with a 2 or larger - a total of 5 points.

+

Consider only horizontal and vertical lines. At how many points do at least two lines overlap?

+
+

Your puzzle answer was 5092.

--- Part Two ---

Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider diagonal lines.

+

Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words:

+
    +
  • An entry like 1,1 -> 3,3 covers points 1,1, 2,2, and 3,3.
  • +
  • An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,9.
  • +
+

Considering all lines from the above example would now produce the following diagram:

+
1.1....11.
+.111...2..
+..2.1.111.
+...1.2.2..
+.112313211
+...1.2....
+..1...1...
+.1.....1..
+1.......1.
+222111....
+
+

You still need to determine the number of points where at least two lines overlap. In the above example, this is still anywhere in the diagram with a 2 or larger - now a total of 12 points.

+

Consider all of the lines. At how many points do at least two lines overlap?

+
+

Your puzzle answer was 20484.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1