From 520a79e175191dcca81e3657fdcb68165615d283 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 | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/2022/day05.hs b/2022/day05.hs index ca99a63..40ee5f1 100644 --- a/2022/day05.hs +++ b/2022/day05.hs @@ -18,18 +18,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 +38,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