From a30d9fbe80fb9f6080dcc360c82ae8a1dd054d1e Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 19 Dec 2022 10:50:20 +0000 Subject: [PATCH] Done day 17 --- advent17/Main.hs | 173 +++++++++++++++++ data/advent17.txt | 1 + data/advent17a.txt | 1 + problems/day17.html | 447 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 622 insertions(+) create mode 100644 advent17/Main.hs create mode 100644 data/advent17.txt create mode 100644 data/advent17a.txt create mode 100644 problems/day17.html diff --git a/advent17/Main.hs b/advent17/Main.hs new file mode 100644 index 0000000..7c2b4d3 --- /dev/null +++ b/advent17/Main.hs @@ -0,0 +1,173 @@ +-- Writeup at https://work.njae.me.uk/2022/12/19/advent-of-code-2022-day-17/ + +-- import Debug.Trace + +import AoC +import qualified Data.Set as S +import qualified Data.Map.Strict as M +import Linear hiding (Trace, trace, distance) +import Control.Lens +import Data.Maybe + +type Position = V2 Int -- x, y; y increasing upwards +type Chamber = S.Set Position +type Rock = S.Set Position + +data SimulationState = SimulationState + { _chamber :: Chamber + , _jets :: [Position] + , _rocks :: [Rock] + , _droppedCount :: Int + } deriving (Eq, Ord) +makeLenses ''SimulationState + +instance Show SimulationState where + show sState = "SimState { _chamber = " + ++ (show $ sState ^. chamber) + ++ ", _jets = " + ++ (show (take 5 (sState ^. jets))) + ++ ", _rocks = " + ++ (show (take 5 (sState ^. rocks))) + ++ ", _droppedCount = " + ++ (show (sState ^. droppedCount)) + ++ " }" + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- readFile dataFileName + let oneJetCycle = mkJets text + print $ part1 oneJetCycle + -- print $ (length oneJetCycle) * (length rockPics) + print $ part2 oneJetCycle + +part1, part2 :: [Position] -> Int +part1 oneJetCycle = rocksHeight final -- fromMaybe -1 $ maximumOf (folded . _y) (final ^. chamber) + where final = simulate mkRocks (cycle oneJetCycle) 2022 + +part2 oneJetCycle = calculatedHeight -- (cycleStartState, cycleRepeatState) + where initState = SimulationState + { _chamber = S.empty + , _jets = (cycle oneJetCycle) + , _rocks = mkRocks + , _droppedCount = 0 + } + (cycleStartState, _) = findEarliestRepeat (length oneJetCycle) initState + cycleRepeatState = findCycleRepeat (length oneJetCycle) cycleStartState + cycleStart = cycleStartState ^. droppedCount + cycleLength = (cycleRepeatState ^. droppedCount) - cycleStart + startHeight = rocksHeight cycleStartState + differenceHeight = (rocksHeight cycleRepeatState) - startHeight + afterStart = 1000000000000 - cycleStart + (numCycles, remainingDrops) = afterStart `divMod` cycleLength + finalState = (!!remainingDrops) $ iterate dropFromTop cycleStartState + finalHeight = rocksHeight finalState + calculatedHeight = finalHeight + (differenceHeight * numCycles) + +rocksHeight :: SimulationState -> Int +rocksHeight state = fromMaybe -1 $ maximumOf (folded . _y) (state ^. chamber) + +simSome :: [Position] -> Int -> Int +simSome oneJetCycle n = fromMaybe -1 $ maximumOf (folded . _y) (final ^. chamber) + where final = simulate mkRocks (cycle oneJetCycle) n + +simulate :: [Rock] -> [Position] -> Int -> SimulationState +simulate rocks jets n = (!!n) $ iterate dropFromTop initState + where initState = SimulationState { _chamber = S.empty, _jets = jets, _rocks = rocks, _droppedCount = 0} + +dropFromTop :: SimulationState -> SimulationState +dropFromTop simState = (dropRock simState (initialPlace simState)) + & rocks %~ tail + & droppedCount %~ (+ 1) + +dropRock :: SimulationState -> Rock -> SimulationState +dropRock simState rock + | rock2 == Nothing = simState & chamber %~ (S.union rock1) + & jets %~ tail + | otherwise = dropRock (simState & jets %~ tail) $ fromJust rock2 + where rock1 = push (simState ^. chamber) rock (head (simState ^. jets)) + rock2 = fall (simState ^. chamber) rock1 + +initialPlace :: SimulationState -> Rock +initialPlace simState = S.map (^+^ (V2 2 startHeight)) rock + where startHeight = 4 + (fromMaybe 0 $ maximumOf (folded . _y) (simState ^. chamber)) + rock = head $ simState ^. rocks + +push :: Chamber -> Rock -> Position -> Rock +push chamber rock direction + -- | trace ("Before push " ++ (intercalate " : " $ [show chamber, show rock, show direction])) False = undefined + | disjoint && inLeft && inRight = pushedRock + | otherwise = rock + where pushedRock = S.map (^+^ direction) rock + disjoint = S.null $ S.intersection pushedRock chamber + inLeft = (fromJust $ minimumOf (folded . _x) pushedRock) >= 0 + inRight = (fromJust $ maximumOf (folded . _x) pushedRock) <= 6 + +fall :: Chamber -> Rock -> Maybe Rock +fall chamber rock + -- | trace ("Before fall " ++ (intercalate " : " $ [show chamber, show rock, show disjoint, show aboveFloor, show droppedRock])) False = undefined + | disjoint && aboveFloor = Just droppedRock + | otherwise = Nothing + where droppedRock = S.map (^+^ (V2 0 -1)) rock + disjoint = S.null $ S.intersection droppedRock chamber + aboveFloor = (fromJust $ minimumOf (folded . _y) droppedRock) > 0 + + +findCycleRepeat :: Int -> SimulationState -> SimulationState +findCycleRepeat jetLength cycleStart = head $ dropWhile (differentProfiles jetLength cycleStart) hares + where hares = drop 1 $ iterate dropFromTop cycleStart + + +findEarliestRepeat :: Int -> SimulationState -> (SimulationState, SimulationState) +findEarliestRepeat jetLength simState = head $ dropWhile (uncurry (differentProfiles jetLength)) pairs + where tortoises = drop 1 $ iterate dropFromTop simState + hares = drop 1 $ iterate (dropFromTop . dropFromTop) simState + pairs = zip tortoises hares + +differentProfiles :: Int -> SimulationState -> SimulationState -> Bool +differentProfiles jetLength t h = (simulationProfile jetLength t) /= (simulationProfile jetLength h) + +simulationProfile :: Int -> SimulationState -> (Chamber, [Position], Rock) +simulationProfile jetLength state = + ( surfaceProfile state + , take jetLength $ state ^. jets + , head $ state ^. rocks + ) + +surfaceProfile :: SimulationState -> Chamber +surfaceProfile state = S.fromList $ map (^-^ (V2 0 peak)) rawProfile + where ch = state ^. chamber + rawProfile = [V2 i (fromMaybe -1 $ maximumOf (folded . filteredBy (_x . only i) . _y) ch) | i <- [0..6] ] + peak = fromJust $ maximumOf (folded . _y) rawProfile + +showChamber :: Chamber -> String +showChamber chamber = unlines + [ [showCell x y | x <- [0..6]] + | y <- reverse [1..yMax] + ] ++ "-------" + where yMax = fromMaybe 0 $ maximumOf (folded . _y) chamber + showCell x y + | (V2 x y) `S.member` chamber = '#' + | otherwise = '.' + +mkJets :: String -> [Position] +mkJets = fmap mkJet + where mkJet '<' = V2 -1 0 + mkJet '>' = V2 1 0 + mkJet _ = error "Illegal jet character" + +mkRocks :: [Rock] +mkRocks = cycle $ fmap mkRock rockPics + +mkRock :: String -> Rock +mkRock rockPic = S.fromList + [ V2 x y + | x <- [0..((length (rockLines!!0)) - 1)] + , y <- [0..((length rockLines) - 1)] + , (rockLines!!y)!!x == '#' + ] + where rockLines = reverse $ lines rockPic + +rockPics :: [String] +rockPics = ["####", ".#.\n###\n.#.", "..#\n..#\n###", "#\n#\n#\n#", "##\n##"] + diff --git a/data/advent17.txt b/data/advent17.txt new file mode 100644 index 0000000..d5ff9e0 --- /dev/null +++ b/data/advent17.txt @@ -0,0 +1 @@ +>>>><<>><<>>><>>>><<<<>>>><<<<>>><<<>><<<<>><<<<>><<<>><>>><<<>><<<<>>><<>><<<>>>><<<<>>><<<>><<<>><<>><<<<>>><<<<><<><<<>>><<<<><<>>>><<>>>><<<<><<<<><<<<>>><<<>><<>>><><<<>>>><<>><<>><<<<>><<>><<><<>>>><<>>><<>>>><<<<>>><>>>><<<>>><<<<>>><>>><<>>>><<<<>><<<><<<<>>>><>><<<<>><<<><>><><<<><>><<<<>>>><<>>>><<>><<<>>>><<<>><<>><<>>>><>>><<<>><<>>><>>>><<><>>>><<<>>><>>>><>>><<<<><<<<><>>>><><<>>>><<<<><<<<><<>><<<>>><<<><<>>><<<><<<>>>><>>>><<<<>>>><>><<>>>><<<>><<<>>><><<<>><<>>>><<<<>>>><<><<><<>>><><<>>><>>>><<><<<><<<>>><<<>><<<>>>><<>>><<<><>>>><<<>>>><<<>><<<<>><>>>><<<<><<><<<<>>><>>>><>><<<<>><<>>>><<<<>>><<<><<<<>>>><<<><<<<>>><><<>>>><<<<>>><<>><><<>>>><<<>>>><<<><>>><<<><<<<>><<<<><<<<>><<<<>>><<>>><<>>>><<<<>><<<<><>>>><<<<>><<<>><<<><<><<<<>>>><<<<>>><><<<>>><><<<>>>><<<>><<<>>>><<<>><><>><<<>><>>>><>>><<><<>><<>>><<<<>><<<>>>><<<>>><<<<>>><>>><><>>>><<<>>>><<<>>>><<<>>>><<<>>>><<><<>>>><<<>><>>>><<<>>>><<>>><<>>><>>>><<>><<<>><<<>>>><><<<<><>>><<><<<>>>><<>>>><><<><<>>><<<<><<<<>><>>>><><<<>>>><<<>>>><><<><<>><><<>>>><<<<>>><>>>><<<><<>><>><<>>><<<<>>><>>>><<<<>><<><<<<>><<>>><<<>><<><>>><<<<>>>><<>>>><<<<>>><<<>>><>><<<<><<>>><>>>><<<<><<<>>>><<<>>>><<>>><>><<<<>><<<<>>>><<<>><<<<><><<<<><<<><<<<>><>>><<<<>><<>>>><<>><<<><<<<>><<><<>>><<<<>>><<<>>>><<>>><>><<<>><<<<>>>><<>>><<>>>><<>><><<>><<<>>>><<<>>>><<>><<<><<<>>>><<<<>><<<>>>><<<>>>><>><<<>><<>>><<><<<<>>>><<<>>>><<<<>><>>>><<>>><<<<><>>>><<<<><>>>><<<>>><<<><>><<<<>>><<<>>><<><<<><<>><<<<>>>><<<<><<<><<<<>><<<<>><><<><<<<>><>><>>><<<<>>>><<><<<>>><<>>>><<>>>><<<<><<>>>><<>>><>>><>>>><<<>>><>>>><<<<><<<<>>>><<<<>>><<>>><<<<><<<><<>>><<><<>><<<>><<<>><<<><<<<>>><<>>>><<<>>>><<>>>><>>>><<<<>>>><<>>>><<<<>><<>>>><<>><><<>>>><>>>><<>>><<>>>><<>>><<><<<>><<>><<<><<<><><>>>><><>>><<<>>><<>><<>><>>><>><>><<>>>><<<<>><><>>><<<<>>><>><<>>><<<>><<>>>><<<<>>><<<><<>>><>>>><<<><<<>><><<<>>><<<>>><>><<<<>>><<<<>><>><><>>><<>><><<>>><><><>>>><>>>><<>><<<><<<>><<<<>><>>><<<<>><<<>><>>>><<<<>>><<<<><<><>><<<><<<>>>><<<<>>><>>>><>>>><<<<><<><>>><<<<>><<<>><>><<<>>>><<<<>>>><<<>>>><<>><<<>><>>><<><<<<>><<<<><>>>><<>><<<>>>><<<<>>>><<><><><><<<<>><<<>>>><<>>>><<>>>><>>>><>>><>>><<<<>><<<>><>>><><>><<<<>>>><<<><<<<>><><<><><<<>>><<<>><<<>>><><<<<>><<<<><>><><<<><<<>>><><<><<<<><<>><<<>>><<<>><<>>><<<<>>>><>>><<<>><<><>>><<<>>>><<<<>><<<>>><<<<>>>><<>>><<<>>><<><<<<>>>><>>>><<>><<<>><<<<><<<>>>><<>>>><<>><<>><<<>>><><<<<>>>><<<<>>>><><<>><<>>><<<<>>><<><<><<><>>><<>>><<<>>><<<>>>><>>><>><<>>>><>>><>><<><<<<>><<<><<<>>>><<>>><<>>><><<<>><<<>>><>><<><<>>><<<>><<>>><><><<><<<>>><<<<>>>><>>>><<<<>><>>>><<<>>><<>>>><<>>>><>>><<<<>><<<<>><<<<>>>><<<<>>>><<<<>><<<<>><<<>>>><><<<>>><<>>><<<>>><<<<>>><<<<>>><<<<><<>>>><<>>><><<<><<<><><>><>><<>>>><<<<>>><>><<<>><<<<>>><<<><<<>><<<<>>>><<<<>><<<<><<<>><<><>>><<<>>><><<>>>><<>><<<>>>><<<>>>><<>>>><<<>>>><<<<>><<><<<<>>><<<<>>>><<<>>>><>>><<<<>>>><<<<>>><<>>>><<<>>>><>>><<><<<<>>>><<><<<>>><<>><<<><<><<<<>>>><<<<><<<>><>><<<>>><>>>><<<<>>><<><<<>>><<>>>><>>><<>>>><>><>><<<<>>>><<<<>>><<<><<<><<<<>>>><<><<<<>>>><<<><<>><<<<>>>><<<<><<<<>><<>>>><<<>><<><<<>><<<>><<<><>><<>><<<>>>><<>>><>>>><<<<>>>><<>>><<<>>><<<<><<>>>><<<<>>>><>><<>><<><<<>>><<>>>><<<><<<<>><<<>>>><<<<>><><<>>><<><<><>>><<<><<>><<<>>><<>>><<<>>>><>>><<<><>>>><>>>><<<>><>>>><<<<><<<>><<>>>><<<>>>><>><<><<<<>>><<<>><<<>><><<>><<><<<<>><>><<<<>>>><<>>>><<<<>>><<<>><>>>><>>><<<<>>><<<>><<<<>><<>>><<<<><<<<><>>><>>>><>><<>><<<>>>><<<<>><<<<><<<<>>>><>>><<>><>><<<<>>><<<>>><<<><>>>><<<><<<><<>><<<<>>>><<<<><<<<>>><><<<<>><<<<>>><<<<><<><<<><>>><<<<>>>><<<>>><<<<>>>><<<<>><<<>><<<<>><<<><<<<>>>><<<<><<><<<<>>>><<>>><<>><<><>>>><<<>>><>><<<><<<<>>>><>>>><<<>>><<<<>>>><>>>><<<<>>>><<<<>><<<<><<><<><><<<<>>><<<<>>>><<<<>><<<>>><>>><<<>>>><<<<>><<<<>>>><<<<><<>>><><<<<>><<><<<>>>><><<<<>><<<<>>><<<>><<>>>><<<><<<<>>><<>>><<>>><>><<>>>><<>>><<<>>><<<<>>><<<<><<>>><<<><<<>>>><<>><<<>>><<<<>>>><<<>>><<<>>><<<><<<<><>>>><>>><<>>><>>>><<<<><<<><<<<>>>><<<<>>><>>>><>><<<<>>><<<<>><<<>><<>><<<<>>><<<<>>><<<<>><<><><<>><<<<>><<<><>>>><>><<>>><><<>><>>>><><<<>>><<<><<<<>>><<><<<>>><<><>>><<<<>><<<>>>><<<>>>><<<>>><<<<>><<<<>>><<<<><<<><<>><>>><<<><>>><>>>><><<>>><>>>><<<<>>><<<<>>><<<>><>><<<>>>><<<><<<>>>><<<<>>><<><<>>><<>>>><>>>><<<<>>><<>>>><<>><<<<>>><<<<><<<><><<<>>><<<<><<<<>>><<<>><<><>>><<<>><<><<<>>>><<<<><<<>>>><<<>>><<><>>>><>><<<>><<<<>>><<<<><<<<>>><<>>>><<><>>><<<>>>><><><<<<>>><<<>>><>>><<<><<<>>><<<><><<<<>>>><>><>>><<<<>><><<>>>><<<>><<<>>>><<<<>><<<><<>>>><>>>><<<>><<<<>><<<<>><>><<<>>>><><<><<><<<><>>><>><<><<>><<<><<<<><<<<>>>><>>>><<<<>>>><<<>><<<>><<>>>><<<>>><<>><<<>>><<>>><<>>>><<<>><<<>>><<<<><<>><>><>><<>>><<><>><<<>>>><<><<>>><<<<><<><<<>>><<<>>><<<>><<<>>>><<>>>><<<<><<>><<<>>><>>><<>><<<><<<><<>>>><>>><<<>><<<<><>><<<<>>>><<<>><<>><<><<>>><><<><>><<<<>>><<>>>><>>><<<>><>><<<><<><<<<>><<<<>><<>>>><<<>>><<<<>>><<<>>><<<><<<<><<<<><<<<>>>><<>>>><<<<>><>>><<<>>><<<<>>><>><<<<>>><<<<>>><><<<<>>>><<<><<<>><<>><>><<><>>>><<<>>>><<<<>><<<>>>><<>>>><<<<><<><<>><<<<>>>><<<<>>>><<<<>>><<<<><<<><>>>><<<<>>>><><<<<>>>><><<<><<>>>><>>><<<<>><>>>><>>>><<<<>>>><<<>><<<>>><<>>>><<<>><>>><<<>><<<<>>>><<<><<>>><<<>>><<>>>><>>><>>>><<<<>>><>><><>>>><><>>>><<>>><><<<<>>>><>><<>>><<<>><<>>>><>><<>><<<>><<<<>>><<<<>>><<<<>>><><<<<>><<><<>><>><<<<>><<><>>><<<>>>><>><<<><<<>><<<<><>>><<<>>><<<<>><<><<<<>>><>>><>>><<>>><<<<>>><<<><<<>>><<<><<<>>>><>>>><<>>>><><<>>>><<>>>><<<<>>>><>>><<<<>><<>>><<><<<<><<<>>><>>>><<<>><<><>><<<>>>><>>>><<<><<>>>><<<<>><<>>>><>>>><<<>>><<<<><<<><<<<><<<<><<<<>><<<>>><<<>><<>>><>>><>>>><<<<>>><<<<>><<<<>><<>><>><<<<><><<><<<>>>><<<><>><<<<>><<<<>>>><<<<>>>><<>>>><<<<>>>><<<<><<<<>><<<>>><<>>>><>>><<<>>><<<>>><<>>>><<>>>><<>><<<>><<>><<<>><<>>><<>>>><>><>><<<<>>><<>><>><<>>>><>>>><<>><<<<>><<<><<>><<<<>>><<<<><<<>>>><<<<><<<>>>><<<>>><<<<>><<<><<<<><<<<>>>><>>>><<>>>><<<>>>><<<<><<><>>>><>><>>><<<>><>>>><>><><<>>><><<>><><<<>><<<>>>><<<>><<<>>>><<<>>>><>><>><<<>>>><><<<<>><<><>><<>>>><<<>>><<<<><<>>><<<<>>>><<>>>><>>>><<>>>><<<>>>><>>>><>><<<<>>><<<<>>><<<<>><<<<>><<<>>><<<>>>><>><>>><<<>><>>><<<><<>>><<>><><<<<>>><><<>>><<><>>><>>><<<<>><<><>>>><<>>><<>>><<<>><<<<>><<>>><<<>>>><<<<>>>><<>>><<>><<<<><<<>>>><<>>><>>><<<>>>><<<>><<<><>>><>>><<<><<>>>><<<<>>><<<><<<>>><<<<><<<>>><<<<>>><<<<>><<<<>><<>>><<<>>>><<<<>>>><<>><<<<>><>><<>><<<>>>><<><><<>><>><<><<>>>><<<><<<<>>><<<><>><<>>><<<<>><<<>><<<<>>><>><><<<>>><><<<<>>>><<<><<<<>>><<<<>><<<><><<<<>>><<>><>>>><<>>><<<<><<>><>><<>>><<><<<<>>>><<><<<>>><<>>>><<<>>>><>>><<<>>><<>><<<<>><>>><<><<<><>>>><>>>><<<<>>>><<<>><<<<>>><<<>>>><<<<><<>>><<<<>><<<><><>>>><<><<>>>><>>>><<><<<>>><<<<><<<<>>><>>><<<>><<>>><>>><<<>>><<<<>>>><<<><>><<<>>><<>>>><>>>><>>>><<<<>>><>><<>>>><>>><<<>>>><><<>>>><<<><<<<>><><<<<>>><><<>><>>>><<>><<<<>>><>><>><>>><<>>><<>>>><><>><<>>>><<<><<<>>><>>><<<<>><<>>>><<<<>>>><<<<>><<<<>>><<>><<>>>><>>>><>>>><><<<><<<<>>><><<><>>>><<<<>><<>><<<>><<<<>>>><<<><<<<>>>><<<>>><<<<>>><>>>><<<<>><<>>><>>><<>>>><<<<>><<<><<<>>>><><<<><<>><<<>>>><<>><<<<>>>><>><<<<>>><<>>><><>>><>><<<>><>><>>>><<<<>><<<>>><<<><<<<>>><<<>>>><<>>>><<><<<<>>><<<<><<<<>><>><<>>><<<<>>><<<>>><<<>>>><<<<>>>><<<>>><<<><<<<><<<>>>><<<<>>>><<<<>>><>>><<<>>><<<<>><<>><<<>><<<>>><<<<>>><<><<<>><<<>>>><<<<>>>><<<<>><<>>>><<<<>>><<>>>><<<<>>><<<<>>>><>>><<<<>>>><<<>>>><<><<<>><<>>><><>>><<><>>>><<<<><<><<<<>>>><<>>>><>>>><<<<><<>>>><<<<><<<<>>><<<>>>><<>>>><>>><><<<<><<<<>>>><>>>><<><<<><>>>><><><<>>>><>>>><>>>><><<<>><<<>>>><<>><<>>>><<><<><<<>>><<<>>>><<>><<<>>>><>>><<<><<<<>>><>>><<<<>>><<><<>>>><<<>><<><<<>>>><<<<>><<<<>>>><<><<><<<>>>><>><<<>><<>>>><<<<>>><<<>>><<<>>><>>><><<<>>><<<>>><<<>><<<<>>>><><<<>>>><<>>><<>>>><>>>><>><>>><>>>><<<><<<<>>><<>><<><<<>>>><<<>>><<><<<>>><<>>>><>><><<>>>><><>><>>>><<>><<<<>><<>>>><>>>><><>>>><<<<>>>><<<<>>>><<>>><<<>><<<<>>><>>><<<<>><<>>><<<<><<<>><<<<>>><<<>>><>><>>><<<<>>>><<<<>><<<>>>><>>><<>>><>>>><<<>><<<>>>><><<<>>>><<<>>>><<<<>>>><<><<><<<>><>>><<<<>>>><<<<>>>><<>>>><<<><<>><>>>><<<<>>><>>>><<<<>><<<>><<>>><<>>>><<>>><><<<<>>><<>>>><<<><<><<>><>><<<>>>><>><<<>>><><>>>><>>><>>>><<<<><<<<>>>><<<<><<<<><<<>>><<<<><<<<>>><<<<>>><<<>>>><<<<>>>><<>>><<>>><<<><<>>><<<<>>><>><<<>>>><<<>><<<<>><<>>><>>><<<>><<<>>>><><<<><<><<>><>>>><<<>><<<><<>>><<<>>>><><<<>><<<>>><<>>><>>><<<<><<<><>>><<<>>>><>><<<<>>>><>>>><><<>>><>><>><<<<><<<>>><<<><<<>>><<<>>><<<<><<>>><<<><<<><<<<><>>><<>>>><<>>>><<>>><<<>>>><<><>><<<><<<<><<>>>><<<<>>>><>>>><<<>><>>>><<<><<>><><<<<><><<<<><>>><<>>>><<<>><>>>><<><><<<<>><<<>>>><<>>>><<<<>><<<<>><<<>><<<><<<<><<><<<>>><>>><<>><<<>><<<>>><>>>><><><<<<>><<><>><<<<>>>><<<<><<<<>>><>>><<<>>><<<><<<><<>><>><<<>><<><<<<>>>><<<>>>><<<>>>><<>>><<><>>><><>>><<<<><<<><<<>>>><<<<>>>><<<<>>>><>><<<><><><<<>>><<<><<<>><<>>>><<<><<<<>>><><<<>>>><>><<<<>>><<<<>>><>>>><<<>>><<<<>><<>><<<<>><<<><>>><<<<>><>><<<><<>><<<>>><<<<>>>><<>><<<>>>><<>><<<><<<<>>><<>>>><><>>>><<>>>><<<<>><>><<>>><<>>><<><<>>>><<<><<<>><><<<>>><<<>>>><<>><>>>><<>><<<<>>><<<><<<><<<><<<>>><<<>><<<>><><><>><<>>>><<<<>>>><<>><<>><<<><<<<>>>><<<<><>><>><>><<<><<<>>>><>><>>>><><<<<><><<<<><<>><<>>>><<<<>>><<<>>><<<<>><<<<><>>>><<>>><<<>>>><>>><<>>><<<>>><<<>><<>>>><<<><<<<>><>><><<<<>>><<>>><<>>><<><<<><<<<>><>><<>><><<<><<><<<<>>><<<<>><<<><<<><<>>>><<><>>><<>>><>>><<>><<>><<<<>>><<<<>>>><>>><>><<<<>>>><<<>><<<<>><<<><<><<<<><<><<<<><>>><<<>>>><<<<><<<>>><>>>><<<<>>>><<<<><<<><><>>>><><<<<>>>><>><>>><>>>><>>><<>><<>>>><<<>>>><<>><><<<><<<><<<><>>>><<<>>>><<<<>>>><>>>><>>>><<>>><<<>>>><<<><<<<>><<<<>>>><<<>>>><<<>><<<><<<><<>>>><<<<><><<<>><<<><>>><<<>><>><<<<>>>><>><<>><<><>><<<>>>><>>><<<>>><<<<>>>><>>>><<>><>>>><>>>><<<>><>>><>>><<>>><<<<><<>>><<<<><<<<><<><<<<>>>><>>>><<<<>>>><<<>>>><<<<>>>><>>>><<<>>>><<<>>><>>>><<<<><<<<>><<<><<>>><<<<><<>>><<>>>><<<>>><<<>>><<>>><>>><<>><<>>>><<<<><<<<>>>><<<>><<<<>>>><>>><<<>>><>>><<>>>><<<>>><<<>><<>><<<>>>><>><<><<>>><>><>>><<<<>>>><<<<>><>><<>><>>><<<<>>><<<<>>>><><<<>>>><<<<>>><<<<>><>>><<>><>><>><<>>><<>>><<<>>>><><<<<>>>><<<>><<<<>>><<<<><><<<<><<>><<<<>><>><<<<>><<>><>>><<>><<>>>><<<>><<<>>>><<<>>><<<>>><<>>><<<<><<>>><<>><<<<>>>><<<<>>>><<<<>>>><< \ No newline at end of file diff --git a/data/advent17a.txt b/data/advent17a.txt new file mode 100644 index 0000000..fb5d89e --- /dev/null +++ b/data/advent17a.txt @@ -0,0 +1 @@ +>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>> \ No newline at end of file diff --git a/problems/day17.html b/problems/day17.html new file mode 100644 index 0000000..685da0b --- /dev/null +++ b/problems/day17.html @@ -0,0 +1,447 @@ + + + + +Day 17 - Advent of Code 2022 + + + + + + + + +

Advent of Code

Neil Smith (AoC++) 34*

   0xffff&2022

+ + + +
+

--- Day 17: Pyroclastic Flow ---

Your handheld device has located an alternative exit from the cave for you and the elephants. The ground is rumbling almost continuously now, but the strange valves bought you some time. It's definitely getting warmer in here, though.

+

The tunnels eventually open into a very tall, narrow chamber. Large, oddly-shaped rocks are falling into the chamber from above, presumably due to all the rumbling. If you can't work out where the rocks will fall next, you might be crushed!

+

The five types of rocks have the following peculiar shapes, where # is rock and . is empty space:

+
####
+
+.#.
+###
+.#.
+
+..#
+..#
+###
+
+#
+#
+#
+#
+
+##
+##
+
+

The rocks fall in the order shown above: first the - shape, then the + shape, and so on. Once the end of the list is reached, the same order repeats: the - shape falls first, sixth, 11th, 16th, etc.

+

The rocks don't spin, but they do get pushed around by jets of hot gas coming out of the walls themselves. A quick scan reveals the effect the jets of hot gas will have on the rocks as they fall (your puzzle input).

+

For example, suppose this was the jet pattern in your cave:

+
>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>
+
+

In jet patterns, < means a push to the left, while > means a push to the right. The pattern above means that the jets will push a falling rock right, then right, then right, then left, then left, then right, and so on. If the end of the list is reached, it repeats.

+

The tall, vertical chamber is exactly seven units wide. Each rock appears so that its left edge is two units away from the left wall and its bottom edge is three units above the highest rock in the room (or the floor, if there isn't one).

+

After a rock appears, it alternates between being pushed by a jet of hot gas one unit (in the direction indicated by the next symbol in the jet pattern) and then falling one unit down. If any movement would cause any part of the rock to move into the walls, floor, or a stopped rock, the movement instead does not occur. If a downward movement would have caused a falling rock to move into the floor or an already-fallen rock, the falling rock stops where it is (having landed on something) and a new rock immediately begins falling.

+

Drawing falling rocks with @ and stopped rocks with #, the jet pattern in the example above manifests as follows:

+
The first rock begins falling:
+|..@@@@.|
+|.......|
+|.......|
+|.......|
++-------+
+
+Jet of gas pushes rock right:
+|...@@@@|
+|.......|
+|.......|
+|.......|
++-------+
+
+Rock falls 1 unit:
+|...@@@@|
+|.......|
+|.......|
++-------+
+
+Jet of gas pushes rock right, but nothing happens:
+|...@@@@|
+|.......|
+|.......|
++-------+
+
+Rock falls 1 unit:
+|...@@@@|
+|.......|
++-------+
+
+Jet of gas pushes rock right, but nothing happens:
+|...@@@@|
+|.......|
++-------+
+
+Rock falls 1 unit:
+|...@@@@|
++-------+
+
+Jet of gas pushes rock left:
+|..@@@@.|
++-------+
+
+Rock falls 1 unit, causing it to come to rest:
+|..####.|
++-------+
+
+A new rock begins falling:
+|...@...|
+|..@@@..|
+|...@...|
+|.......|
+|.......|
+|.......|
+|..####.|
++-------+
+
+Jet of gas pushes rock left:
+|..@....|
+|.@@@...|
+|..@....|
+|.......|
+|.......|
+|.......|
+|..####.|
++-------+
+
+Rock falls 1 unit:
+|..@....|
+|.@@@...|
+|..@....|
+|.......|
+|.......|
+|..####.|
++-------+
+
+Jet of gas pushes rock right:
+|...@...|
+|..@@@..|
+|...@...|
+|.......|
+|.......|
+|..####.|
++-------+
+
+Rock falls 1 unit:
+|...@...|
+|..@@@..|
+|...@...|
+|.......|
+|..####.|
++-------+
+
+Jet of gas pushes rock left:
+|..@....|
+|.@@@...|
+|..@....|
+|.......|
+|..####.|
++-------+
+
+Rock falls 1 unit:
+|..@....|
+|.@@@...|
+|..@....|
+|..####.|
++-------+
+
+Jet of gas pushes rock right:
+|...@...|
+|..@@@..|
+|...@...|
+|..####.|
++-------+
+
+Rock falls 1 unit, causing it to come to rest:
+|...#...|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+A new rock begins falling:
+|....@..|
+|....@..|
+|..@@@..|
+|.......|
+|.......|
+|.......|
+|...#...|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+

The moment each of the next few rocks begins falling, you would see this:

+
|..@....|
+|..@....|
+|..@....|
+|..@....|
+|.......|
+|.......|
+|.......|
+|..#....|
+|..#....|
+|####...|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+|..@@...|
+|..@@...|
+|.......|
+|.......|
+|.......|
+|....#..|
+|..#.#..|
+|..#.#..|
+|#####..|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+|..@@@@.|
+|.......|
+|.......|
+|.......|
+|....##.|
+|....##.|
+|....#..|
+|..#.#..|
+|..#.#..|
+|#####..|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+|...@...|
+|..@@@..|
+|...@...|
+|.......|
+|.......|
+|.......|
+|.####..|
+|....##.|
+|....##.|
+|....#..|
+|..#.#..|
+|..#.#..|
+|#####..|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+|....@..|
+|....@..|
+|..@@@..|
+|.......|
+|.......|
+|.......|
+|..#....|
+|.###...|
+|..#....|
+|.####..|
+|....##.|
+|....##.|
+|....#..|
+|..#.#..|
+|..#.#..|
+|#####..|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+|..@....|
+|..@....|
+|..@....|
+|..@....|
+|.......|
+|.......|
+|.......|
+|.....#.|
+|.....#.|
+|..####.|
+|.###...|
+|..#....|
+|.####..|
+|....##.|
+|....##.|
+|....#..|
+|..#.#..|
+|..#.#..|
+|#####..|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+|..@@...|
+|..@@...|
+|.......|
+|.......|
+|.......|
+|....#..|
+|....#..|
+|....##.|
+|....##.|
+|..####.|
+|.###...|
+|..#....|
+|.####..|
+|....##.|
+|....##.|
+|....#..|
+|..#.#..|
+|..#.#..|
+|#####..|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+|..@@@@.|
+|.......|
+|.......|
+|.......|
+|....#..|
+|....#..|
+|....##.|
+|##..##.|
+|######.|
+|.###...|
+|..#....|
+|.####..|
+|....##.|
+|....##.|
+|....#..|
+|..#.#..|
+|..#.#..|
+|#####..|
+|..###..|
+|...#...|
+|..####.|
++-------+
+
+

To prove to the elephants your simulation is accurate, they want to know how tall the tower will get after 2022 rocks have stopped (but before the 2023rd rock begins falling). In this example, the tower of rocks will be 3068 units tall.

+

How many units tall will the tower of rocks be after 2022 rocks have stopped falling?

+
+

Your puzzle answer was 3211.

--- Part Two ---

The elephants are not impressed by your simulation. They demand to know how tall the tower will be after 1000000000000 rocks have stopped! Only then will they feel confident enough to proceed through the cave.

+

In the example above, the tower would be 1514285714288 units tall!

+

How tall will the tower be after 1000000000000 rocks have stopped?

+
+

Your puzzle answer was 1589142857183.

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