Files
advent-of-code-2022/src/Day5/Part2.hs

25 lines
793 B
Haskell
Raw Normal View History

2022-12-05 15:59:01 +01:00
module Day5.Part2 (
execute9001Operation,
day5_2
) where
import qualified Data.Sequence as S
import Data.Foldable (toList)
import Day5.Shared
execute9001Operation :: Row -> Operation -> Row
execute9001Operation row op
= updatedRow
where
originStack = row `S.index` asInt (from op)
targetStack = row `S.index` asInt (to op)
movedContent = take (count op) $ content originStack
updatedOrigin = Stack (drop (count op) $ content originStack)
updatedTarget = Stack (movedContent ++ content targetStack)
updatedRow = S.update (asInt $ to op) updatedTarget $ S.update (asInt $ from op) updatedOrigin row
day5_2 :: String -> String
day5_2 input = toList . findTopOfStacks $ foldl execute9001Operation row operations
where
(row, operations) = parseInput input