diff --git a/2022/day05.hs b/2022/day05.hs index 88d3bea..d15908a 100644 --- a/2022/day05.hs +++ b/2022/day05.hs @@ -2,7 +2,7 @@ import Data.List (transpose, delete) import Data.List.Split (chunksOf, splitOn) import Data.Char (isSpace) -data Command = Move {amount :: Int, from :: Int, to :: Int} deriving Show +data Command = Move Int Int Int type State = [[Char]] @@ -18,14 +18,14 @@ listApp 0 f (x:xs) = f x : xs listApp n f (x:xs) = x : listApp (n-1) f xs moveN :: Command -> State -> State -moveN (Move 0 from to) = id -moveN (Move n from to) = moveN (Move (n-1) from to) . move (Move 1 from to) +moveN (Move 0 _ _) = id +moveN (Move n f t) = moveN (Move (n-1) f t) . move (Move 1 f t) move :: Command -> State -> State -move (Move amount from to) state = addTo $ popFrom state where - popFrom = listApp from (drop amount) - elems = take amount (state !! from) - addTo = listApp to (elems ++ ) +move (Move n f t) state = addTo $ popFrom state where + popFrom = listApp f (drop n) + elems = take n (state !! f) + addTo = listApp t (elems ++ ) main :: IO () main = do