Compare commits
No commits in common. "c83d1a920e6ff5e72b8c03c3dbab601d70f10102" and "9208e5d2b9b812d989756714b5194323729d6a8f" have entirely different histories.
c83d1a920e
...
9208e5d2b9
58
day5/main.hs
58
day5/main.hs
@ -1,58 +0,0 @@
|
|||||||
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
|
|
21
day6/main.hs
21
day6/main.hs
@ -1,21 +0,0 @@
|
|||||||
import System.IO
|
|
||||||
import Data.List
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
marker :: Int -> Int -> String -> Int
|
|
||||||
marker p n s@(_:xs)
|
|
||||||
| unique == n = p+n
|
|
||||||
| otherwise = marker (p+1) n xs
|
|
||||||
where
|
|
||||||
unique = length $ nub $ take n s
|
|
||||||
|
|
||||||
|
|
||||||
handler :: String -> String
|
|
||||||
handler s = (show $ marker 0 4 s) ++ "\n" ++
|
|
||||||
(show $ marker 0 14 s) ++ "\n"
|
|
||||||
|
|
||||||
main :: IO ()
|
|
||||||
main = do
|
|
||||||
interact handler
|
|
Loading…
x
Reference in New Issue
Block a user