From 81f9c61ac7fe5482ba3210eede266450c4e562b6 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 5 Dec 2022 10:16:44 +0000 Subject: [PATCH] Done day 5 --- advent-of-code22.cabal | 5 + advent05/Main.hs | 124 ++++++++++ data/advent05.txt | 512 +++++++++++++++++++++++++++++++++++++++++ data/advent05a.txt | 9 + problems/day05.html | 203 ++++++++++++++++ 5 files changed, 853 insertions(+) create mode 100644 advent05/Main.hs create mode 100644 data/advent05.txt create mode 100644 data/advent05a.txt create mode 100644 problems/day05.html diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index ca41874..7a73e22 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -114,3 +114,8 @@ executable advent04i import: common-extensions, build-directives main-is: advent04/Main-interval.hs build-depends: text, attoparsec, intervals + +executable advent05 + import: common-extensions, build-directives + main-is: advent05/Main.hs + build-depends: text, attoparsec, containers diff --git a/advent05/Main.hs b/advent05/Main.hs new file mode 100644 index 0000000..9ec1d25 --- /dev/null +++ b/advent05/Main.hs @@ -0,0 +1,124 @@ +-- Writeup at https://work.njae.me.uk/2022/12/04/advent-of-code-2022-day-4/ + +import System.Environment +import Data.Text (Text) +import qualified Data.Text.IO as TIO +import Data.Attoparsec.Text hiding (take) +import Control.Applicative +import Data.List +import Data.Maybe +import qualified Data.IntMap.Strict as M +import Data.IntMap.Strict ((!), (!?)) + +data Crate = Crate Char deriving (Show, Eq) +type Wharf = M.IntMap [Crate] + +data Move = Move Int Int Int -- quantity, from, to + deriving (Show, Eq) + + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- TIO.readFile dataFileName + let ((wharfLines, colNames), moves) = successfulParse text + -- print wharfLines + -- print colNames + -- print moves + let wharf = makeWharf wharfLines colNames + -- print wharf + -- print $ applyMove wharf (head moves) + putStrLn $ part1 wharf moves + putStrLn $ part2 wharf moves + + +getDataFileName :: IO String +getDataFileName = + do args <- getArgs + progName <- getProgName + let baseDataName = if null args + then progName + else head args + let dataFileName = "data/" ++ baseDataName ++ ".txt" + return dataFileName + + +part1 :: Wharf -> [Move] -> String +part1 wharf moves = showTops $ applyMoves1 wharf moves + +part2 :: Wharf -> [Move] -> String +part2 wharf moves = showTops $ applyMoves2 wharf moves + +showTops :: Wharf -> String +showTops wharf = fmap extractName $ fmap (head . snd) $ M.toAscList wharf + +extractName :: Crate -> Char +extractName (Crate c) = c + +makeWharf :: [[Maybe Crate]] -> [Int] -> Wharf +makeWharf wharfLines colNames = M.fromList $ zip colNames wharfCols + where wharfCols = fmap (fmap fromJust) + $ fmap (dropWhile isNothing) + $ transpose wharfLines + +applyMoves1 :: Wharf -> [Move] -> Wharf +applyMoves1 wharf moves = foldl' applyMove1 wharf moves + +applyMove1 :: Wharf -> Move -> Wharf +applyMove1 wharf m@(Move n _ _) = foldl' makeMove1 wharf (replicate n m) + +makeMove1 :: Wharf -> Move -> Wharf +makeMove1 wharf (Move _ from to) = M.insert from origin + $ M.insert to destination wharf + where (c:origin) = wharf!from + destination = c:(fromMaybe [] $ wharf!?to) + +applyMoves2 :: Wharf -> [Move] -> Wharf +applyMoves2 wharf moves = foldl' applyMove2 wharf moves + +applyMove2 :: Wharf -> Move -> Wharf +applyMove2 wharf (Move n from to) = M.insert from origin' + $ M.insert to destination wharf + where origin = wharf!from + moving = take n origin + origin' = drop n origin + destination = moving ++ (fromMaybe [] $ wharf!?to) + + +-- Parse the input file + +problemP :: Parser (([[Maybe Crate]], [Int]), [Move]) +wharfP :: Parser ([[Maybe Crate]], [Int]) +wharfLineP :: Parser [Maybe Crate] +wharfCellP, blankP, crateP :: Parser (Maybe Crate) +stackLabelsP :: Parser [Int] +movesP :: Parser [Move] +moveP :: Parser Move + +-- problemP = (,) <$> wharfP <* endOfLine <* endOfLine <*> movesP +problemP = (,) <$> wharfP <*> movesP + +wharfP = (,) <$> (wharfLineP `sepBy` endOfLine) <*> stackLabelsP + +wharfLineP = wharfCellP `sepBy1` (char ' ') + +wharfCellP = crateP <|> blankP +blankP = Nothing <$ (count 3 space) +crateP = (Just . Crate) <$> ("[" *> letter) <* "]" + +-- stackLabelsP = ((many1 space) *> (decimal `sepBy` (many1 space))) <* (takeWhile1 isHorizontalSpace) +stackLabelsP = (many1 space) + *> (decimal `sepBy` (many1 space)) + <* (many1 space) + +movesP = moveP `sepBy` endOfLine +moveP = Move <$> ("move " *> decimal) + <*> (" from " *> decimal) + <*> (" to " *> decimal) + + +successfulParse :: Text -> (([[Maybe Crate]], [Int]), [Move]) +successfulParse input = + case parseOnly problemP input of + Left _err -> (([], []), []) -- TIO.putStr $ T.pack $ parseErrorPretty err + Right problem -> problem diff --git a/data/advent05.txt b/data/advent05.txt new file mode 100644 index 0000000..fb137f9 --- /dev/null +++ b/data/advent05.txt @@ -0,0 +1,512 @@ +[M] [N] [Z] +[F] [R] [Z] [C] [C] +[C] [V] [L] [N] [G] [V] +[W] [L] [T] [H] [V] [F] [H] +[T] [T] [W] [F] [B] [P] [J] [L] +[D] [L] [H] [J] [C] [G] [S] [R] [M] +[L] [B] [C] [P] [S] [D] [M] [Q] [P] +[B] [N] [J] [S] [Z] [W] [F] [W] [R] + 1 2 3 4 5 6 7 8 9 + +move 5 from 3 to 6 +move 2 from 2 to 5 +move 1 from 9 to 1 +move 1 from 3 to 1 +move 5 from 7 to 5 +move 2 from 9 to 8 +move 1 from 2 to 8 +move 1 from 4 to 2 +move 8 from 1 to 6 +move 4 from 6 to 9 +move 1 from 2 to 1 +move 2 from 4 to 8 +move 2 from 8 to 4 +move 3 from 7 to 5 +move 6 from 5 to 3 +move 1 from 1 to 8 +move 1 from 5 to 7 +move 5 from 6 to 9 +move 3 from 5 to 8 +move 2 from 4 to 3 +move 1 from 7 to 8 +move 2 from 8 to 6 +move 2 from 1 to 8 +move 8 from 3 to 8 +move 11 from 6 to 3 +move 1 from 4 to 7 +move 1 from 3 to 7 +move 2 from 6 to 1 +move 7 from 9 to 7 +move 10 from 3 to 5 +move 1 from 9 to 3 +move 2 from 9 to 5 +move 5 from 5 to 2 +move 19 from 8 to 6 +move 1 from 9 to 6 +move 1 from 3 to 8 +move 4 from 2 to 6 +move 1 from 1 to 4 +move 5 from 8 to 9 +move 1 from 2 to 1 +move 6 from 7 to 2 +move 3 from 5 to 8 +move 3 from 8 to 1 +move 2 from 9 to 6 +move 1 from 7 to 8 +move 6 from 2 to 7 +move 1 from 4 to 8 +move 3 from 8 to 4 +move 2 from 1 to 5 +move 7 from 7 to 6 +move 1 from 7 to 2 +move 3 from 4 to 6 +move 2 from 9 to 2 +move 1 from 1 to 8 +move 2 from 1 to 3 +move 1 from 8 to 7 +move 3 from 2 to 5 +move 5 from 5 to 8 +move 4 from 5 to 3 +move 1 from 7 to 8 +move 2 from 8 to 1 +move 1 from 8 to 5 +move 5 from 3 to 5 +move 13 from 5 to 1 +move 1 from 3 to 4 +move 2 from 8 to 3 +move 3 from 1 to 4 +move 1 from 3 to 1 +move 1 from 8 to 1 +move 5 from 1 to 9 +move 1 from 3 to 7 +move 2 from 9 to 6 +move 2 from 1 to 7 +move 3 from 1 to 5 +move 3 from 1 to 5 +move 1 from 6 to 1 +move 4 from 4 to 3 +move 3 from 9 to 1 +move 5 from 1 to 7 +move 7 from 7 to 8 +move 1 from 3 to 9 +move 28 from 6 to 8 +move 5 from 5 to 9 +move 6 from 6 to 1 +move 4 from 1 to 8 +move 5 from 9 to 1 +move 12 from 8 to 7 +move 1 from 3 to 8 +move 6 from 1 to 4 +move 5 from 4 to 1 +move 3 from 6 to 4 +move 2 from 3 to 4 +move 3 from 1 to 5 +move 6 from 7 to 1 +move 2 from 4 to 9 +move 2 from 5 to 4 +move 19 from 8 to 1 +move 4 from 9 to 5 +move 5 from 4 to 3 +move 4 from 1 to 4 +move 5 from 5 to 1 +move 3 from 8 to 5 +move 7 from 7 to 3 +move 14 from 1 to 8 +move 5 from 4 to 2 +move 12 from 8 to 7 +move 1 from 3 to 6 +move 3 from 5 to 9 +move 1 from 7 to 8 +move 8 from 1 to 2 +move 5 from 1 to 2 +move 9 from 3 to 4 +move 8 from 4 to 6 +move 2 from 1 to 9 +move 3 from 6 to 1 +move 5 from 6 to 7 +move 14 from 7 to 1 +move 1 from 4 to 7 +move 6 from 8 to 2 +move 14 from 1 to 4 +move 13 from 4 to 9 +move 2 from 3 to 5 +move 3 from 1 to 7 +move 1 from 8 to 4 +move 1 from 4 to 1 +move 1 from 1 to 3 +move 1 from 3 to 4 +move 1 from 4 to 1 +move 1 from 6 to 9 +move 1 from 7 to 6 +move 1 from 4 to 5 +move 11 from 9 to 3 +move 6 from 3 to 8 +move 5 from 3 to 1 +move 2 from 8 to 4 +move 1 from 6 to 2 +move 7 from 9 to 2 +move 1 from 7 to 2 +move 1 from 9 to 8 +move 2 from 8 to 6 +move 30 from 2 to 3 +move 2 from 7 to 2 +move 2 from 8 to 2 +move 3 from 8 to 7 +move 6 from 2 to 5 +move 1 from 2 to 5 +move 3 from 1 to 8 +move 2 from 6 to 7 +move 1 from 1 to 9 +move 1 from 9 to 3 +move 7 from 3 to 1 +move 6 from 7 to 8 +move 8 from 3 to 9 +move 7 from 9 to 1 +move 1 from 5 to 8 +move 7 from 5 to 9 +move 2 from 4 to 2 +move 11 from 3 to 6 +move 2 from 2 to 7 +move 11 from 1 to 8 +move 2 from 5 to 4 +move 11 from 6 to 4 +move 12 from 4 to 9 +move 4 from 1 to 5 +move 3 from 7 to 9 +move 12 from 8 to 4 +move 1 from 1 to 7 +move 6 from 8 to 3 +move 2 from 3 to 5 +move 3 from 8 to 4 +move 3 from 3 to 7 +move 9 from 9 to 7 +move 5 from 3 to 9 +move 1 from 3 to 2 +move 13 from 7 to 5 +move 1 from 2 to 6 +move 1 from 6 to 1 +move 1 from 1 to 6 +move 16 from 4 to 5 +move 1 from 5 to 6 +move 16 from 5 to 4 +move 13 from 4 to 5 +move 3 from 4 to 2 +move 1 from 6 to 7 +move 3 from 2 to 1 +move 8 from 5 to 2 +move 3 from 1 to 4 +move 1 from 7 to 9 +move 14 from 5 to 1 +move 10 from 1 to 5 +move 1 from 2 to 8 +move 19 from 9 to 1 +move 1 from 9 to 1 +move 6 from 2 to 7 +move 4 from 1 to 7 +move 1 from 8 to 6 +move 16 from 5 to 3 +move 1 from 5 to 4 +move 2 from 5 to 2 +move 1 from 5 to 6 +move 1 from 6 to 5 +move 1 from 2 to 4 +move 7 from 7 to 2 +move 4 from 4 to 7 +move 2 from 6 to 2 +move 8 from 2 to 9 +move 4 from 9 to 2 +move 16 from 3 to 7 +move 4 from 9 to 7 +move 14 from 1 to 3 +move 26 from 7 to 8 +move 1 from 5 to 4 +move 20 from 8 to 4 +move 5 from 1 to 8 +move 2 from 4 to 6 +move 4 from 3 to 2 +move 1 from 6 to 5 +move 8 from 2 to 4 +move 1 from 6 to 5 +move 1 from 7 to 8 +move 8 from 3 to 1 +move 6 from 1 to 9 +move 1 from 3 to 6 +move 14 from 4 to 1 +move 1 from 3 to 8 +move 2 from 2 to 1 +move 1 from 6 to 8 +move 1 from 2 to 8 +move 5 from 8 to 1 +move 2 from 1 to 6 +move 2 from 5 to 9 +move 1 from 6 to 3 +move 1 from 6 to 1 +move 5 from 9 to 2 +move 5 from 4 to 1 +move 5 from 4 to 2 +move 16 from 1 to 8 +move 9 from 1 to 4 +move 24 from 8 to 6 +move 1 from 8 to 7 +move 7 from 6 to 5 +move 1 from 3 to 4 +move 3 from 1 to 8 +move 3 from 5 to 8 +move 10 from 4 to 8 +move 3 from 4 to 6 +move 1 from 7 to 4 +move 20 from 6 to 7 +move 1 from 4 to 9 +move 1 from 4 to 9 +move 7 from 2 to 3 +move 13 from 8 to 9 +move 4 from 5 to 9 +move 4 from 8 to 5 +move 18 from 9 to 2 +move 14 from 7 to 5 +move 6 from 3 to 8 +move 1 from 3 to 2 +move 1 from 8 to 6 +move 4 from 8 to 2 +move 1 from 2 to 3 +move 17 from 5 to 3 +move 18 from 3 to 5 +move 6 from 7 to 2 +move 3 from 9 to 7 +move 1 from 8 to 6 +move 5 from 2 to 5 +move 26 from 2 to 7 +move 1 from 6 to 9 +move 29 from 7 to 9 +move 15 from 5 to 2 +move 1 from 6 to 7 +move 8 from 9 to 2 +move 14 from 2 to 6 +move 16 from 9 to 1 +move 6 from 9 to 1 +move 1 from 7 to 1 +move 3 from 2 to 1 +move 5 from 2 to 6 +move 15 from 1 to 4 +move 1 from 2 to 8 +move 1 from 9 to 7 +move 1 from 8 to 6 +move 19 from 6 to 7 +move 10 from 1 to 8 +move 4 from 8 to 3 +move 1 from 7 to 5 +move 3 from 5 to 3 +move 13 from 7 to 6 +move 2 from 8 to 9 +move 7 from 3 to 6 +move 5 from 5 to 3 +move 1 from 1 to 6 +move 2 from 5 to 1 +move 4 from 4 to 8 +move 7 from 8 to 7 +move 8 from 7 to 3 +move 1 from 8 to 4 +move 2 from 9 to 2 +move 8 from 6 to 5 +move 1 from 4 to 5 +move 4 from 5 to 4 +move 2 from 2 to 8 +move 9 from 4 to 5 +move 2 from 1 to 9 +move 2 from 8 to 9 +move 14 from 6 to 4 +move 5 from 3 to 4 +move 3 from 9 to 7 +move 3 from 5 to 3 +move 2 from 4 to 8 +move 2 from 4 to 7 +move 2 from 8 to 9 +move 4 from 5 to 8 +move 16 from 4 to 6 +move 1 from 9 to 6 +move 3 from 7 to 5 +move 7 from 7 to 5 +move 10 from 5 to 1 +move 6 from 3 to 8 +move 2 from 9 to 3 +move 3 from 6 to 9 +move 3 from 3 to 6 +move 2 from 1 to 7 +move 13 from 6 to 2 +move 2 from 4 to 5 +move 2 from 7 to 6 +move 2 from 6 to 7 +move 2 from 4 to 1 +move 3 from 9 to 5 +move 1 from 1 to 4 +move 3 from 2 to 5 +move 2 from 4 to 1 +move 2 from 3 to 2 +move 5 from 8 to 5 +move 1 from 7 to 2 +move 1 from 7 to 1 +move 1 from 3 to 5 +move 1 from 8 to 7 +move 1 from 6 to 7 +move 1 from 3 to 5 +move 12 from 5 to 6 +move 6 from 6 to 2 +move 1 from 7 to 4 +move 1 from 5 to 7 +move 2 from 8 to 9 +move 1 from 9 to 6 +move 1 from 8 to 9 +move 5 from 6 to 9 +move 1 from 8 to 1 +move 14 from 2 to 4 +move 1 from 7 to 1 +move 1 from 7 to 2 +move 3 from 2 to 3 +move 2 from 3 to 4 +move 1 from 2 to 4 +move 4 from 6 to 2 +move 8 from 5 to 8 +move 15 from 4 to 8 +move 3 from 4 to 8 +move 7 from 8 to 4 +move 6 from 1 to 3 +move 1 from 6 to 1 +move 5 from 4 to 8 +move 7 from 9 to 1 +move 1 from 5 to 6 +move 4 from 2 to 6 +move 10 from 1 to 8 +move 29 from 8 to 3 +move 1 from 4 to 5 +move 1 from 4 to 6 +move 6 from 1 to 4 +move 1 from 5 to 8 +move 3 from 4 to 2 +move 27 from 3 to 7 +move 18 from 7 to 9 +move 5 from 6 to 3 +move 7 from 7 to 4 +move 1 from 7 to 8 +move 9 from 3 to 5 +move 5 from 3 to 6 +move 3 from 4 to 2 +move 1 from 7 to 2 +move 2 from 8 to 4 +move 2 from 8 to 6 +move 2 from 8 to 6 +move 8 from 2 to 1 +move 7 from 5 to 4 +move 1 from 8 to 9 +move 4 from 1 to 5 +move 1 from 2 to 9 +move 8 from 6 to 3 +move 3 from 1 to 8 +move 1 from 1 to 7 +move 8 from 3 to 6 +move 2 from 8 to 3 +move 1 from 3 to 6 +move 4 from 6 to 7 +move 16 from 4 to 2 +move 1 from 3 to 5 +move 2 from 6 to 4 +move 1 from 2 to 3 +move 2 from 7 to 3 +move 2 from 7 to 8 +move 3 from 6 to 7 +move 4 from 5 to 2 +move 2 from 4 to 2 +move 4 from 9 to 8 +move 3 from 5 to 1 +move 3 from 1 to 6 +move 6 from 9 to 1 +move 4 from 7 to 9 +move 8 from 9 to 5 +move 4 from 5 to 2 +move 7 from 8 to 6 +move 11 from 6 to 8 +move 4 from 1 to 2 +move 3 from 8 to 9 +move 5 from 8 to 7 +move 2 from 1 to 6 +move 4 from 5 to 6 +move 2 from 7 to 9 +move 2 from 7 to 3 +move 5 from 6 to 2 +move 4 from 3 to 1 +move 1 from 7 to 2 +move 1 from 3 to 2 +move 2 from 6 to 7 +move 1 from 1 to 6 +move 6 from 9 to 6 +move 1 from 7 to 6 +move 1 from 7 to 6 +move 2 from 1 to 7 +move 2 from 8 to 6 +move 4 from 9 to 2 +move 17 from 2 to 6 +move 1 from 9 to 4 +move 1 from 1 to 3 +move 1 from 4 to 1 +move 20 from 2 to 8 +move 2 from 7 to 6 +move 2 from 2 to 5 +move 1 from 3 to 1 +move 1 from 2 to 5 +move 6 from 8 to 6 +move 2 from 5 to 6 +move 3 from 6 to 4 +move 1 from 1 to 4 +move 15 from 8 to 2 +move 11 from 2 to 9 +move 1 from 1 to 3 +move 10 from 9 to 4 +move 1 from 9 to 8 +move 12 from 6 to 3 +move 1 from 8 to 7 +move 1 from 5 to 4 +move 8 from 4 to 7 +move 5 from 3 to 4 +move 7 from 6 to 4 +move 3 from 3 to 6 +move 3 from 3 to 2 +move 1 from 3 to 6 +move 17 from 4 to 3 +move 1 from 3 to 4 +move 2 from 4 to 9 +move 14 from 3 to 6 +move 2 from 2 to 7 +move 1 from 4 to 9 +move 8 from 7 to 6 +move 1 from 3 to 4 +move 9 from 6 to 2 +move 1 from 4 to 2 +move 26 from 6 to 2 +move 27 from 2 to 6 +move 10 from 2 to 4 +move 1 from 7 to 6 +move 28 from 6 to 2 +move 21 from 2 to 4 +move 2 from 6 to 7 +move 3 from 2 to 1 +move 5 from 6 to 5 +move 3 from 5 to 2 +move 1 from 7 to 4 +move 11 from 2 to 4 +move 21 from 4 to 9 +move 1 from 5 to 8 +move 1 from 8 to 6 +move 18 from 9 to 7 +move 1 from 5 to 7 +move 3 from 9 to 8 +move 1 from 6 to 7 +move 1 from 3 to 5 +move 1 from 8 to 3 +move 22 from 7 to 5 +move 13 from 5 to 1 +move 16 from 4 to 5 +move 3 from 1 to 4 +move 2 from 3 to 9 +move 3 from 9 to 7 +move 6 from 4 to 6 +move 1 from 4 to 2 +move 2 from 7 to 3 \ No newline at end of file diff --git a/data/advent05a.txt b/data/advent05a.txt new file mode 100644 index 0000000..e98aba4 --- /dev/null +++ b/data/advent05a.txt @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2 \ No newline at end of file diff --git a/problems/day05.html b/problems/day05.html new file mode 100644 index 0000000..5019c7a --- /dev/null +++ b/problems/day05.html @@ -0,0 +1,203 @@ + + + + +Day 5 - Advent of Code 2022 + + + + + + + + +

Advent of Code

Neil Smith (AoC++) 10*

  0.0.0.0:2022

+ + + +
+

--- Day 5: Supply Stacks ---

The expedition can depart as soon as the final supplies have been unloaded from the ships. Supplies are stored in stacks of marked crates, but because the needed supplies are buried under many other crates, the crates need to be rearranged.

+

The ship has a giant cargo crane capable of moving crates between stacks. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack.

+

The Elves don't want to interrupt the crane operator during this delicate procedure, but they forgot to ask her which crate will end up where, and they want to be ready to unload them as soon as possible so they can embark.

+

They do, however, have a drawing of the starting stacks of crates and the rearrangement procedure (your puzzle input). For example:

+
    [D]    
+[N] [C]    
+[Z] [M] [P]
+ 1   2   3 
+
+move 1 from 2 to 1
+move 3 from 1 to 3
+move 2 from 2 to 1
+move 1 from 1 to 2
+
+

In this example, there are three stacks of crates. Stack 1 contains two crates: crate Z is on the bottom, and crate N is on top. Stack 2 contains three crates; from bottom to top, they are crates M, C, and D. Finally, stack 3 contains a single crate, P.

+

Then, the rearrangement procedure is given. In each step of the procedure, a quantity of crates is moved from one stack to a different stack. In the first step of the above rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this configuration:

+
[D]        
+[N] [C]    
+[Z] [M] [P]
+ 1   2   3 
+
+

In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one at a time, so the first crate to be moved (D) ends up below the second and third crates:

+
        [Z]
+        [N]
+    [C] [D]
+    [M] [P]
+ 1   2   3
+
+

Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one at a time, crate C ends up below crate M:

+
        [Z]
+        [N]
+[M]     [D]
+[C]     [P]
+ 1   2   3
+
+

Finally, one crate is moved from stack 1 to stack 2:

+
        [Z]
+        [N]
+        [D]
+[C] [M] [P]
+ 1   2   3
+
+

The Elves just need to know which crate will end up on top of each stack; in this example, the top crates are C in stack 1, M in stack 2, and Z in stack 3, so you should combine these together and give the Elves the message CMZ.

+

After the rearrangement procedure completes, what crate ends up on top of each stack?

+
+

Your puzzle answer was TGWSMRBPN.

--- Part Two ---

As you watch the crane operator expertly rearrange the crates, you notice the process isn't following your prediction.

+

Some mud was covering the writing on the side of the crane, and you quickly wipe it away. The crane isn't a CrateMover 9000 - it's a CrateMover 9001.

+

The CrateMover 9001 is notable for many new and exciting features: air conditioning, leather seats, an extra cup holder, and the ability to pick up and move multiple crates at once.

+

Again considering the example above, the crates begin in the same configuration:

+
    [D]    
+[N] [C]    
+[Z] [M] [P]
+ 1   2   3 
+
+

Moving a single crate from stack 2 to stack 1 behaves the same as before:

+
[D]        
+[N] [C]    
+[Z] [M] [P]
+ 1   2   3 
+
+

However, the action of moving three crates from stack 1 to stack 3 means that those three moved crates stay in the same order, resulting in this new configuration:

+
        [D]
+        [N]
+    [C] [Z]
+    [M] [P]
+ 1   2   3
+
+

Next, as both crates are moved from stack 2 to stack 1, they retain their order as well:

+
        [D]
+        [N]
+[C]     [Z]
+[M]     [P]
+ 1   2   3
+
+

Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate C that gets moved:

+
        [D]
+        [N]
+        [Z]
+[M] [C] [P]
+ 1   2   3
+
+

In this example, the CrateMover 9001 has put the crates in a totally different order: MCD.

+

Before the rearrangement process finishes, update your simulation so that the Elves know where they should stand to be ready to unload the final supplies. After the rearrangement procedure completes, what crate ends up on top of each stack?

+
+

Your puzzle answer was TZLTLWRNF.

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