This commit is contained in:
Oskar Winkels 2022-12-08 13:35:51 +01:00
parent 9208e5d2b9
commit dbda70f3cb
Signed by: o.winkels
GPG Key ID: E7484A06E99DAEF1
2 changed files with 59 additions and 1 deletions

View File

@ -1,4 +1,4 @@
DAY = 4
DAY = 5
DIR = day$(DAY)

58
day5/main.hs Normal file
View File

@ -0,0 +1,58 @@
import System.IO
import Data.List
import Data.List.Split
parseStacks :: [String] -> [[Char]]
parseStacks = map (filter (/=' ') . init) . transpose . map (map (!!1) . chunksOf 4)
parseMove :: String -> (Int,Int,Int)
parseMove s = (read (x!!1) :: Int, read (x!!3) :: Int, read (x!!5) :: Int)
where x = splitOn " " s
applyStacks :: (Int,[Char]) -> (Int,[Char]) -> (Int,[Char]) -> [Char]
applyStacks (f,fs) (t,ts) (c,cs)
| c == f = fs
| c == t = ts
| otherwise = cs
applyMove :: [[Char]] -> (Int,Int,Int) -> [[Char]]
applyMove acc (n,f,t) = map (applyStacks (f,fromstack) (t,newstack)) $ zip [1..] acc
where oldstack = acc !! (f-1)
c = take n oldstack
fromstack = drop n oldstack
tostack = acc !! (t-1)
newstack = c ++ tostack
applyMoves :: [[Char]] -> (Int,Int,Int) -> [[Char]]
applyMoves acc (0,_,_) = acc
applyMoves acc (n,f,l) = applyMoves (applyMove acc (1,f,l)) (n-1,f,l)
applyMoves2 :: [[Char]] -> (Int,Int,Int) -> [[Char]]
applyMoves2 acc move = applyMove acc move
handler :: String -> String
handler s = (show $ map head $ foldl applyMoves stacks moves) ++ "\n" ++
(show $ map head $ foldl applyMoves2 stacks moves) ++ "\n"
where sections = splitOn [""] $ lines s
stacks = parseStacks $ sections !! 0
moves = map parseMove $ sections !! 1
main :: IO ()
main = do
interact handler