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

26 lines
880 B
Haskell
Raw Normal View History

2022-12-05 15:49:32 +01:00
module Day5.Part1 (
executeOperation,
day5_1
) where
import qualified Data.Sequence as S
import Data.Foldable (toList)
2022-12-05 15:59:01 +01:00
import Day5.Shared
2022-12-05 15:49:32 +01:00
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
2022-12-05 15:59:01 +01:00
day5_1 input = toList . findTopOfStacks $ foldl executeOperation row operations
2022-12-05 15:49:32 +01:00
where
(row, operations) = parseInput input