1 -- Writeup at https://work.njae.me.uk/2022/12/04/advent-of-code-2022-day-4/
4 import Data.Text (Text)
5 import qualified Data.Text.IO as TIO
6 import Data.Attoparsec.Text hiding (take)
7 import Control.Applicative
10 import qualified Data.IntMap.Strict as M
11 import Data.IntMap.Strict ((!), (!?))
13 data Crate = Crate Char deriving (Show, Eq)
14 type Wharf = M.IntMap [Crate]
16 data Move = Move Int Int Int -- quantity, from, to
22 do dataFileName <- getDataFileName
23 text <- TIO.readFile dataFileName
24 let ((wharfLines, colNames), moves) = successfulParse text
25 let wharf = makeWharf wharfLines colNames
26 putStrLn $ part1 wharf moves
27 putStrLn $ part2 wharf moves
29 part1 :: Wharf -> [Move] -> String
30 part1 wharf moves = showTops $ applyMoves1 wharf moves
32 part2 :: Wharf -> [Move] -> String
33 part2 wharf moves = showTops $ applyMoves2 wharf moves
35 showTops :: Wharf -> String
36 showTops wharf = fmap extractName $ fmap (head . snd) $ M.toAscList wharf
38 extractName :: Crate -> Char
39 extractName (Crate c) = c
41 makeWharf :: [[Maybe Crate]] -> [Int] -> Wharf
42 makeWharf wharfLines colNames = M.fromList $ zip colNames wharfCols
43 where wharfCols = fmap catMaybes $ transpose wharfLines
45 applyMoves1 :: Wharf -> [Move] -> Wharf
46 applyMoves1 wharf moves = foldl' applyMove1 wharf moves
48 applyMove1 :: Wharf -> Move -> Wharf
49 applyMove1 wharf m@(Move n _ _) = foldl' makeMove1 wharf (replicate n m)
51 makeMove1 :: Wharf -> Move -> Wharf
52 makeMove1 wharf (Move _ from to) = M.insert from origin
53 $ M.insert to destination wharf
54 where (c:origin) = wharf!from
55 destination = c:(fromMaybe [] $ wharf!?to)
57 applyMoves2 :: Wharf -> [Move] -> Wharf
58 applyMoves2 wharf moves = foldl' applyMove2 wharf moves
60 applyMove2 :: Wharf -> Move -> Wharf
61 applyMove2 wharf (Move n from to) = M.insert from origin'
62 $ M.insert to destination wharf
63 where origin = wharf!from
64 moving = take n origin
65 origin' = drop n origin
66 destination = moving ++ (fromMaybe [] $ wharf!?to)
69 -- Parse the input file
71 problemP :: Parser (([[Maybe Crate]], [Int]), [Move])
72 wharfP :: Parser ([[Maybe Crate]], [Int])
73 wharfLineP :: Parser [Maybe Crate]
74 wharfCellP, blankP, crateP :: Parser (Maybe Crate)
75 stackLabelsP :: Parser [Int]
76 movesP :: Parser [Move]
79 -- problemP = (,) <$> wharfP <* endOfLine <* endOfLine <*> movesP
80 problemP = (,) <$> wharfP <*> movesP
82 wharfP = (,) <$> (wharfLineP `sepBy` endOfLine) <*> stackLabelsP
84 wharfLineP = wharfCellP `sepBy1` (char ' ')
86 wharfCellP = crateP <|> blankP
87 blankP = Nothing <$ (count 3 space)
88 crateP = (Just . Crate) <$> ("[" *> letter) <* "]"
90 -- stackLabelsP = ((many1 space) *> (decimal `sepBy` (many1 space))) <* (takeWhile1 isHorizontalSpace)
91 stackLabelsP = (many1 space)
92 *> (decimal `sepBy` (many1 space))
95 movesP = moveP `sepBy` endOfLine
96 moveP = Move <$> ("move " *> decimal)
97 <*> (" from " *> decimal)
98 <*> (" to " *> decimal)
101 successfulParse :: Text -> (([[Maybe Crate]], [Int]), [Move])
102 successfulParse input =
103 case parseOnly problemP input of
104 Left _err -> (([], []), []) -- TIO.putStr $ T.pack $ parseErrorPretty err
105 Right problem -> problem