Files
advent-of-code-2022/src/Day5/Part1.hs
Jens Kadenbach d62259bf75 Day 5 Part 2
2022-12-05 15:59:01 +01:00

26 行
880 B
Haskell

module Day5.Part1 (
executeOperation,
day5_1
) where
import qualified Data.Sequence as S
import Data.Foldable (toList)
import Day5.Shared
executeOperation :: Row -> Operation -> Row
executeOperation row op
| count op == 1 = updatedRow
| otherwise = executeOperation updatedRow (Operation { count = count op - 1, from = from op, to = to op })
where
originStack = row `S.index` asInt (from op)
targetStack = row `S.index` asInt (to op)
movedContent = head $ content originStack
updatedOrigin = Stack (drop 1 $ content originStack)
updatedTarget = Stack (movedContent : content targetStack)
updatedRow = S.update (asInt $ to op) updatedTarget $ S.update (asInt $ from op) updatedOrigin row
day5_1 :: String -> String
day5_1 input = toList . findTopOfStacks $ foldl executeOperation row operations
where
(row, operations) = parseInput input