Refactor part 1 to use part 2

This commit is contained in:
Quinten Kock 2022-12-05 16:16:01 +01:00
parent 5920477193
commit d493d56d28
1 changed files with 4 additions and 11 deletions

View File

@ -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