From d493d56d281ff8641d465a1ff17a8256339cb615 Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Mon, 5 Dec 2022 16:16:01 +0100 Subject: [PATCH] Refactor part 1 to use part 2 --- 2022/day05.hs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/2022/day05.hs b/2022/day05.hs index ca99a63..88d3bea 100644 --- a/2022/day05.hs +++ b/2022/day05.hs @@ -1,7 +1,6 @@ import Data.List (transpose, delete) import Data.List.Split (chunksOf, splitOn) import Data.Char (isSpace) -import Debug.Trace (traceShowId, traceShow) data Command = Move {amount :: Int, from :: Int, to :: Int} deriving Show @@ -18,18 +17,12 @@ listApp :: Int -> (a -> a) -> [a] -> [a] listApp 0 f (x:xs) = f x : xs listApp n f (x:xs) = x : listApp (n-1) f xs -move1 :: (Int, Int) -> State -> State -move1 (from, to) state = addTo $ popFrom state where - popFrom = listApp from tail - elem = head (state !! from) - addTo = listApp to (elem:) - moveN :: Command -> State -> State moveN (Move 0 from to) = id -moveN (Move n from to) = moveN (Move (n-1) from to) . move1 (from,to) +moveN (Move n from to) = moveN (Move (n-1) from to) . move (Move 1 from to) -moveN2 :: Command -> State -> State -moveN2 (Move amount from to) state = addTo $ popFrom state where +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 ++ ) @@ -44,4 +37,4 @@ main = do print $ map head $ foldl (flip moveN) init cmds putStr "part 2: " - print $ map head $ foldl (flip moveN2) init cmds + print $ map head $ foldl (flip move) init cmds