do
content <- input 5
let run = Aoc.run content
run (pure "CNSZFDVLJ") (strategy reverse)
run (pure "QNDWLMGNS") (strategy id)
where
strategy a = map head . uncurry (foldl' $ flip (runProcedure a)) . bimap parseStack procedures . sections
type Stack = [Crate]
type Crate = Char
data Procedure = Move Int Int Int
deriving Show
doLift (i - 1) stacks
where
(head x, (tail x):xs)
doLift i (x:xs) = second (x:) $ doLift (i - 1) xs
doLift (i - 1) stacks
where
(liftStrat . take count $ x, (drop count x):xs)
doLift i (x:xs) = second (x:) $ doLift (i - 1) xs
doDrop (i - 1) stacks
where
(crates ++ x) : xs
doDrop i (x:xs) = x : doDrop (i - 1) xs
doDrop _ [] = []
dropCrate to lifted remainder
where
(lifted, remainder) = liftNCrate f count from stack
move f count from to stack
bimap (init) (drop 1) . break null . lines
map (dropWhile (==' ')) . transpose . parseStrategy row
parseStrategy procedureParser
map fst . map (head . readP_to_S a)
-- moves <- procedures . snd . sections <$> sample 5
-- stacks <- parseStack . fst . sections <$> sample 5
-- foldl' (\a x -> runProcedure x a) stacks moves
map head
-- Parsers
between (char '[') (char ']') get
do
count 3 (satisfy isSpace)
pure ' '
crate <|> emptyStack
manyTill (stack <* (optional (char ' '))) eof
-- Procedure parser
do
string "move "
amount <- read <$> munch1 isDigit
string " from "
from <- read <$> munch1 isDigit
string " to "
to <- read <$> munch1 isDigit
pure $ Move amount from to