Skip to content
Snippets Groups Projects
Commit 0dbe7d9a authored by Felix Valentini's avatar Felix Valentini
Browse files

solved part one of day 5

parent 7741dd1b
No related branches found
No related tags found
No related merge requests found
{-# LANGUAGE MultiWayIf #-}
import Data.Char
import Data.List
-- input depth: 8
-- sample depth: 3
parseStack :: Int -> IO [(Int, String)]
parseStack depth = (zip [1 .. ] <$> map (filter (/= ' ')))
parseStack :: Int -> IO [String]
parseStack depth = map (filter (/= ' '))
. filter (any isUpper) . (transpose <$> take depth) . lines
<$> readFile "./input.txt"
-- newer copy shit from the internet fucked up my parser!!
numbers :: String -> [Int]
numbers [] = []
numbers (l:ls) = if ord l >= 48 && ord l <= 57
then (ord l - 48) : numbers ls
else numbers ls
numbers' :: String -> [Int]
numbers' = map (read :: String -> Int) . words . filter (not . isAlpha)
-- designed for lists == length 3
tuplify3 :: [a] -> (a, a, a)
tuplify3 (x1:x2:x3:_) = (x1, x2, x3)
......@@ -23,20 +29,41 @@ dropFirst 0 xs = xs
dropFirst n (_:xs) = dropFirst (n-1) xs
-- input depth: 10
-- input depth: 5
-- sample depth: 5
parseMoves :: Int -> IO [(Int, Int, Int)]
parseMoves depth = readFile "./input.txt"
>>= (return . map (tuplify3 . numbers)) . (dropFirst depth <$> lines)
>>= (return . map (adjustToListIndices . tuplify3 . numbers')) . (dropFirst depth <$> lines)
adjustToListIndices :: (Int, Int, Int) -> (Int, Int, Int)
adjustToListIndices (times, from, to) = (times, (from - 1), (to - 1))
move :: Int -> Int -> [String] -> [String]
move from to l = map (\x -> if
| x == from -> (tail (l !! from))
| x == to -> prepend ((l !! from) !! 0) (l !! to)
| otherwise -> l !! x) [0..(length l) - 1]
nMove :: (Int, Int, Int) -> [String] -> [String]
nMove (1, from, to) l = move from to l
nMove (times, from, to) l = nMove (times - 1, from, to) $ move from to l
----------------------------------------------------------------------------------------------------
moves :: [(Int, Int, Int)] -> [String] -> [String]
moves [x] l = nMove x l
moves (x:xs) l = moves xs (nMove x l)
-- must be in range of the stack, otherwise it throws an exception
lookupStack :: Int -> [(Int, String)]-> String
lookupStack n l = snd $ head $ filter (\(k, _) -> k == n) l
solution1 :: [String] -> String
solution1 = map (head)
move :: (Int, Int, Int) -> [(Int, String)] -> String
move (times, from, to) = undefined
prepend :: a -> [a] -> [a]
prepend a xs = a : xs
fib :: Int -> Int
fib 0 = 1
fib n = n * (n - 1)
-- To solve I need a foldr
-- e.x. nMove (1, 0, 1) (nMove (2, 1, 0) (nMove (3, 0, 2) (nMove (1, 1, 0) stack)))
main :: IO ()
main = undefined
main = pure (moves) <*> parseMoves 10 <*> parseStack 8 >>= print
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment