From a02f60a5bc25537437380fdcda1097019c75c38a Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 15 Dec 2022 15:13:27 +0000 Subject: [PATCH] Done day 14 --- advent-of-code22.cabal | 5 + advent14/Main.hs | 108 +++++++++++++++++ data/advent14.txt | 148 ++++++++++++++++++++++++ data/advent14a.txt | 2 + problems/day14.html | 256 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 519 insertions(+) create mode 100644 advent14/Main.hs create mode 100644 data/advent14.txt create mode 100644 data/advent14a.txt create mode 100644 problems/day14.html diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index 012bec2..6859245 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -165,3 +165,8 @@ executable advent13 import: common-extensions, build-directives main-is: advent13/Main.hs build-depends: text, attoparsec + +executable advent14 + import: common-extensions, build-directives + main-is: advent14/Main.hs + build-depends: text, attoparsec, containers, linear, lens diff --git a/advent14/Main.hs b/advent14/Main.hs new file mode 100644 index 0000000..0ded02b --- /dev/null +++ b/advent14/Main.hs @@ -0,0 +1,108 @@ +-- Writeup at https://work.njae.me.uk/2022/12/15/advent-of-code-2022-day-14/ + +import AoC +import Data.Text (Text) +import qualified Data.Text.IO as TIO +import Data.Attoparsec.Text hiding (take, D) +import Control.Applicative +import Data.List +import Data.Ix +import Data.Maybe +import qualified Data.Set as S +import Linear -- hiding (Trace, trace, distance) +import Control.Lens + +type Position = V2 Int -- x, y, y increasing down. + +type Cave = S.Set Position + +data Sand = Falling Position | Blocked Position | Escaped + deriving (Eq, Show) + +-- open floor: sand escapes below given level +-- closed floor: sand blocked by floor with this y +data Floor = Open Int | Closed Int deriving (Eq, Show) + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- TIO.readFile dataFileName + let corners = successfulParse text + let stone = mkCave corners + let floorY = fromJust $ maximumOf (folded . _y) stone + print $ part1 stone floorY + print $ part2 stone floorY + +part1, part2 :: Cave -> Int -> Int +part1 stone floorY = sandQty + where filledCave = dropManySand stone (Open floorY) + sandQty = (S.size filledCave) - (S.size stone) +part2 stone floorY = sandQty + where filledCave = dropManySand stone (Closed (floorY + 2)) + sandQty = (S.size filledCave) - (S.size stone) + +mkCave :: [[Position]] -> Cave +mkCave walls = foldl' addWall S.empty walls + +addWall :: Cave -> [Position] -> Cave +addWall cave wall = foldl' addSegment cave segments + where segments = zip wall $ tail wall + +addSegment :: Cave -> (Position, Position) -> Cave +addSegment cave segment = S.union cave segments + where segments = S.fromList $ range (uncurry min segment, uncurry max segment) + +fallDirections :: [Position] +fallDirections = [V2 0 1, V2 -1 1, V2 1 1] + +sandOrigin :: Position +sandOrigin = V2 500 0 + +fallStep :: Sand -> Cave -> Floor -> Sand +fallStep (Blocked here) _ _ = Blocked here +fallStep Escaped _ _ = Escaped +fallStep (Falling here) cave (Open floorY) + | here ^. _y > floorY = Escaped + | otherwise = maybe (Blocked here) Falling $ find vacant + $ fmap (here ^+^) fallDirections + where vacant there = there `S.notMember` cave +fallStep (Falling here) cave (Closed floorY) = + maybe (Blocked here) Falling $ find vacant $ fmap (here ^+^) fallDirections + where vacant there = (there ^. _y < floorY) && (there `S.notMember` cave) + +fallsTo :: Sand -> Cave -> Floor -> Sand +fallsTo here cave floorY = + case fallStep here cave floorY of + Escaped -> Escaped + Blocked there -> Blocked there + Falling there -> fallsTo (Falling there) cave floorY + +dropManySand :: Cave -> Floor -> Cave +dropManySand cave floorY + | sandOrigin `S.member` cave = cave + | otherwise = case dropOneSand cave floorY of + Nothing -> cave + Just cave' -> dropManySand cave' floorY + +dropOneSand :: Cave -> Floor -> Maybe Cave +dropOneSand cave floorY = + case (fallsTo (Falling sandOrigin) cave floorY) of + Escaped -> Nothing + Blocked there -> Just (S.insert there cave) + Falling _ -> error "sand still falling" + +-- Parse the input file + +wallsP :: Parser [[Position]] +wallP :: Parser [Position] +cornerP :: Parser Position + +wallsP = wallP `sepBy` endOfLine +wallP = cornerP `sepBy` " -> " +cornerP = V2 <$> (decimal <* ",") <*> decimal + +successfulParse :: Text -> [[Position]] +successfulParse input = + case parseOnly wallsP input of + Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right corners -> corners diff --git a/data/advent14.txt b/data/advent14.txt new file mode 100644 index 0000000..573c29d --- /dev/null +++ b/data/advent14.txt @@ -0,0 +1,148 @@ +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +460,159 -> 460,154 -> 460,159 -> 462,159 -> 462,151 -> 462,159 -> 464,159 -> 464,152 -> 464,159 +464,109 -> 464,111 -> 463,111 -> 463,118 -> 473,118 -> 473,111 -> 468,111 -> 468,109 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +478,33 -> 478,37 -> 471,37 -> 471,41 -> 483,41 -> 483,37 -> 482,37 -> 482,33 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +466,96 -> 470,96 +494,22 -> 499,22 +499,13 -> 499,16 -> 498,16 -> 498,19 -> 507,19 -> 507,16 -> 501,16 -> 501,13 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +460,145 -> 460,146 -> 462,146 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +460,159 -> 460,154 -> 460,159 -> 462,159 -> 462,151 -> 462,159 -> 464,159 -> 464,152 -> 464,159 +454,168 -> 458,168 +476,60 -> 476,61 -> 489,61 +502,26 -> 507,26 +447,106 -> 452,106 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +498,24 -> 503,24 +478,33 -> 478,37 -> 471,37 -> 471,41 -> 483,41 -> 483,37 -> 482,37 -> 482,33 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +499,13 -> 499,16 -> 498,16 -> 498,19 -> 507,19 -> 507,16 -> 501,16 -> 501,13 +457,166 -> 461,166 +478,33 -> 478,37 -> 471,37 -> 471,41 -> 483,41 -> 483,37 -> 482,37 -> 482,33 +464,109 -> 464,111 -> 463,111 -> 463,118 -> 473,118 -> 473,111 -> 468,111 -> 468,109 +499,13 -> 499,16 -> 498,16 -> 498,19 -> 507,19 -> 507,16 -> 501,16 -> 501,13 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +478,33 -> 478,37 -> 471,37 -> 471,41 -> 483,41 -> 483,37 -> 482,37 -> 482,33 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +457,99 -> 461,99 +499,13 -> 499,16 -> 498,16 -> 498,19 -> 507,19 -> 507,16 -> 501,16 -> 501,13 +463,162 -> 467,162 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +457,104 -> 462,104 +450,104 -> 455,104 +476,60 -> 476,61 -> 489,61 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +462,143 -> 473,143 -> 473,142 +483,44 -> 483,48 -> 481,48 -> 481,55 -> 489,55 -> 489,48 -> 487,48 -> 487,44 +463,99 -> 467,99 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +478,33 -> 478,37 -> 471,37 -> 471,41 -> 483,41 -> 483,37 -> 482,37 -> 482,33 +472,168 -> 476,168 +472,123 -> 472,124 -> 476,124 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +483,44 -> 483,48 -> 481,48 -> 481,55 -> 489,55 -> 489,48 -> 487,48 -> 487,44 +489,30 -> 494,30 +462,143 -> 473,143 -> 473,142 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +482,30 -> 487,30 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +460,145 -> 460,146 -> 462,146 +460,168 -> 464,168 +460,159 -> 460,154 -> 460,159 -> 462,159 -> 462,151 -> 462,159 -> 464,159 -> 464,152 -> 464,159 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +492,28 -> 497,28 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +464,109 -> 464,111 -> 463,111 -> 463,118 -> 473,118 -> 473,111 -> 468,111 -> 468,109 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +466,164 -> 470,164 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +464,109 -> 464,111 -> 463,111 -> 463,118 -> 473,118 -> 473,111 -> 468,111 -> 468,109 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +478,33 -> 478,37 -> 471,37 -> 471,41 -> 483,41 -> 483,37 -> 482,37 -> 482,33 +460,96 -> 464,96 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +472,123 -> 472,124 -> 476,124 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +460,159 -> 460,154 -> 460,159 -> 462,159 -> 462,151 -> 462,159 -> 464,159 -> 464,152 -> 464,159 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +499,13 -> 499,16 -> 498,16 -> 498,19 -> 507,19 -> 507,16 -> 501,16 -> 501,13 +464,109 -> 464,111 -> 463,111 -> 463,118 -> 473,118 -> 473,111 -> 468,111 -> 468,109 +463,166 -> 467,166 +483,44 -> 483,48 -> 481,48 -> 481,55 -> 489,55 -> 489,48 -> 487,48 -> 487,44 +491,24 -> 496,24 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +488,26 -> 493,26 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +483,44 -> 483,48 -> 481,48 -> 481,55 -> 489,55 -> 489,48 -> 487,48 -> 487,44 +460,159 -> 460,154 -> 460,159 -> 462,159 -> 462,151 -> 462,159 -> 464,159 -> 464,152 -> 464,159 +499,13 -> 499,16 -> 498,16 -> 498,19 -> 507,19 -> 507,16 -> 501,16 -> 501,13 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +453,102 -> 458,102 +485,28 -> 490,28 +461,106 -> 466,106 +454,106 -> 459,106 +464,109 -> 464,111 -> 463,111 -> 463,118 -> 473,118 -> 473,111 -> 468,111 -> 468,109 +478,33 -> 478,37 -> 471,37 -> 471,41 -> 483,41 -> 483,37 -> 482,37 -> 482,33 +503,30 -> 508,30 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +466,90 -> 470,90 +460,159 -> 460,154 -> 460,159 -> 462,159 -> 462,151 -> 462,159 -> 464,159 -> 464,152 -> 464,159 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +460,164 -> 464,164 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +496,30 -> 501,30 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +472,96 -> 476,96 +510,30 -> 515,30 +463,93 -> 467,93 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +475,99 -> 479,99 +466,168 -> 470,168 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +469,99 -> 473,99 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +464,109 -> 464,111 -> 463,111 -> 463,118 -> 473,118 -> 473,111 -> 468,111 -> 468,109 +499,13 -> 499,16 -> 498,16 -> 498,19 -> 507,19 -> 507,16 -> 501,16 -> 501,13 +483,44 -> 483,48 -> 481,48 -> 481,55 -> 489,55 -> 489,48 -> 487,48 -> 487,44 +495,26 -> 500,26 +460,159 -> 460,154 -> 460,159 -> 462,159 -> 462,151 -> 462,159 -> 464,159 -> 464,152 -> 464,159 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +499,28 -> 504,28 +483,44 -> 483,48 -> 481,48 -> 481,55 -> 489,55 -> 489,48 -> 487,48 -> 487,44 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +483,44 -> 483,48 -> 481,48 -> 481,55 -> 489,55 -> 489,48 -> 487,48 -> 487,44 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 +469,93 -> 473,93 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +506,28 -> 511,28 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +469,166 -> 473,166 +460,159 -> 460,154 -> 460,159 -> 462,159 -> 462,151 -> 462,159 -> 464,159 -> 464,152 -> 464,159 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +468,137 -> 468,132 -> 468,137 -> 470,137 -> 470,130 -> 470,137 -> 472,137 -> 472,136 -> 472,137 -> 474,137 -> 474,127 -> 474,137 -> 476,137 -> 476,131 -> 476,137 -> 478,137 -> 478,130 -> 478,137 -> 480,137 -> 480,128 -> 480,137 -> 482,137 -> 482,128 -> 482,137 -> 484,137 -> 484,133 -> 484,137 +466,74 -> 466,71 -> 466,74 -> 468,74 -> 468,64 -> 468,74 -> 470,74 -> 470,65 -> 470,74 -> 472,74 -> 472,68 -> 472,74 -> 474,74 -> 474,68 -> 474,74 -> 476,74 -> 476,71 -> 476,74 -> 478,74 -> 478,65 -> 478,74 -> 480,74 -> 480,73 -> 480,74 -> 482,74 -> 482,64 -> 482,74 +461,87 -> 461,80 -> 461,87 -> 463,87 -> 463,80 -> 463,87 -> 465,87 -> 465,80 -> 465,87 -> 467,87 -> 467,86 -> 467,87 \ No newline at end of file diff --git a/data/advent14a.txt b/data/advent14a.txt new file mode 100644 index 0000000..1926028 --- /dev/null +++ b/data/advent14a.txt @@ -0,0 +1,2 @@ +498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9 \ No newline at end of file diff --git a/problems/day14.html b/problems/day14.html new file mode 100644 index 0000000..213205c --- /dev/null +++ b/problems/day14.html @@ -0,0 +1,256 @@ + + + + +Day 14 - Advent of Code 2022 + + + + + + + + +

Advent of Code

Neil Smith (AoC++) 28*

   var y=2022;

+ + + +
+

--- Day 14: Regolith Reservoir ---

The distress signal leads you to a giant waterfall! Actually, hang on - the signal seems like it's coming from the waterfall itself, and that doesn't make any sense. However, you do notice a little path that leads behind the waterfall.

+

Correction: the distress signal leads you behind a giant waterfall! There seems to be a large cave system here, and the signal definitely leads further inside.

+

As you begin to make your way deeper underground, you feel the ground rumble for a moment. Sand begins pouring into the cave! If you don't quickly figure out where the sand is going, you could quickly become trapped!

+

Fortunately, your familiarity with analyzing the path of falling material will come in handy here. You scan a two-dimensional vertical slice of the cave above you (your puzzle input) and discover that it is mostly air with structures made of rock.

+

Your scan traces the path of each solid rock structure and reports the x,y coordinates that form the shape of the path, where x represents distance to the right and y represents distance down. Each path appears as a single line of text in your scan. After the first point of each path, each point indicates the end of a straight horizontal or vertical line to be drawn from the previous point. For example:

+
498,4 -> 498,6 -> 496,6
+503,4 -> 502,4 -> 502,9 -> 494,9
+
+

This scan means that there are two paths of rock; the first path consists of two straight lines, and the second path consists of three straight lines. (Specifically, the first path consists of a line of rock from 498,4 through 498,6 and another line of rock from 498,6 through 496,6.)

+

The sand is pouring into the cave from point 500,0.

+

Drawing rock as #, air as ., and the source of the sand as +, this becomes:

+

+  4     5  5
+  9     0  0
+  4     0  3
+0 ......+...
+1 ..........
+2 ..........
+3 ..........
+4 ....#...##
+5 ....#...#.
+6 ..###...#.
+7 ........#.
+8 ........#.
+9 #########.
+
+

Sand is produced one unit at a time, and the next unit of sand is not produced until the previous unit of sand comes to rest. A unit of sand is large enough to fill one tile of air in your scan.

+

A unit of sand always falls down one step if possible. If the tile immediately below is blocked (by rock or sand), the unit of sand attempts to instead move diagonally one step down and to the left. If that tile is blocked, the unit of sand attempts to instead move diagonally one step down and to the right. Sand keeps moving as long as it is able to do so, at each step trying to move down, then down-left, then down-right. If all three possible destinations are blocked, the unit of sand comes to rest and no longer moves, at which point the next unit of sand is created back at the source.

+

So, drawing sand that has come to rest as o, the first unit of sand simply falls straight down and then stops:

+
......+...
+..........
+..........
+..........
+....#...##
+....#...#.
+..###...#.
+........#.
+......o.#.
+#########.
+
+

The second unit of sand then falls straight down, lands on the first one, and then comes to rest to its left:

+
......+...
+..........
+..........
+..........
+....#...##
+....#...#.
+..###...#.
+........#.
+.....oo.#.
+#########.
+
+

After a total of five units of sand have come to rest, they form this pattern:

+
......+...
+..........
+..........
+..........
+....#...##
+....#...#.
+..###...#.
+......o.#.
+....oooo#.
+#########.
+
+

After a total of 22 units of sand:

+
......+...
+..........
+......o...
+.....ooo..
+....#ooo##
+....#ooo#.
+..###ooo#.
+....oooo#.
+...ooooo#.
+#########.
+
+

Finally, only two more units of sand can possibly come to rest:

+
......+...
+..........
+......o...
+.....ooo..
+....#ooo##
+...o#ooo#.
+..###ooo#.
+....oooo#.
+.o.ooooo#.
+#########.
+
+

Once all 24 units of sand shown above have come to rest, all further sand flows out the bottom, falling into the endless void. Just for fun, the path any new sand takes before falling forever is shown here with ~:

+
.......+...
+.......~...
+......~o...
+.....~ooo..
+....~#ooo##
+...~o#ooo#.
+..~###ooo#.
+..~..oooo#.
+.~o.ooooo#.
+~#########.
+~..........
+~..........
+~..........
+
+

Using your scan, simulate the falling sand. How many units of sand come to rest before sand starts flowing into the abyss below?

+
+

Your puzzle answer was 644.

--- Part Two ---

You realize you misread the scan. There isn't an endless void at the bottom of the scan - there's floor, and you're standing on it!

+

You don't have time to scan the floor, so assume the floor is an infinite horizontal line with a y coordinate equal to two plus the highest y coordinate of any point in your scan.

+

In the example above, the highest y coordinate of any point is 9, and so the floor is at y=11. (This is as if your scan contained one extra rock path like -infinity,11 -> infinity,11.) With the added floor, the example above now looks like this:

+
        ...........+........
+        ....................
+        ....................
+        ....................
+        .........#...##.....
+        .........#...#......
+        .......###...#......
+        .............#......
+        .............#......
+        .....#########......
+        ....................
+<-- etc #################### etc -->
+
+

To find somewhere safe to stand, you'll need to simulate falling sand until a unit of sand comes to rest at 500,0, blocking the source entirely and stopping the flow of sand into the cave. In the example above, the situation finally looks like this after 93 units of sand come to rest:

+
............o............
+...........ooo...........
+..........ooooo..........
+.........ooooooo.........
+........oo#ooo##o........
+.......ooo#ooo#ooo.......
+......oo###ooo#oooo......
+.....oooo.oooo#ooooo.....
+....oooooooooo#oooooo....
+...ooo#########ooooooo...
+..ooooo.......ooooooooo..
+#########################
+
+

Using your scan, simulate the falling sand until the source of the sand becomes blocked. How many units of sand come to rest?

+
+

Your puzzle answer was 27324.

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