Compare commits

...

1 Commits

Author SHA1 Message Date
Quinten Kock 520a79e175 Refactor part 1 to use part 2 2022-12-05 16:16:01 +01:00
1 changed files with 4 additions and 10 deletions

View File

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