Skip to content
Snippets Groups Projects
Commit c3e71b58 authored by flex99's avatar flex99
Browse files

solved part 2 of day 5

parent 0dbe7d9a
Branches
No related tags found
No related merge requests found
......@@ -3,11 +3,9 @@
import Data.Char
import Data.List
-- input depth: 8
-- sample depth: 3
parseStack :: Int -> IO [String]
parseStack depth = map (filter (/= ' '))
. filter (any isUpper) . (transpose <$> take depth) . lines
parseStack :: IO [String]
parseStack = map (filter (/= ' '))
. filter (any isUpper) . (transpose <$> take 8) . lines
<$> readFile "./input.txt"
-- newer copy shit from the internet fucked up my parser!!
......@@ -24,46 +22,52 @@ numbers' = map (read :: String -> Int) . words . filter (not . isAlpha)
tuplify3 :: [a] -> (a, a, a)
tuplify3 (x1:x2:x3:_) = (x1, x2, x3)
dropFirst :: Int -> [String] -> [String]
dropFirst :: Int -> [a] -> [a]
dropFirst 0 xs = xs
dropFirst n (_:xs) = dropFirst (n-1) xs
-- input depth: 10
-- sample depth: 5
parseMoves :: Int -> IO [(Int, Int, Int)]
parseMoves depth = readFile "./input.txt"
>>= (return . map (adjustToListIndices . tuplify3 . numbers')) . (dropFirst depth <$> lines)
parseMoves :: IO [(Int, Int, Int)]
parseMoves = readFile "./input.txt"
>>= (return . map (adjustToListIndices . tuplify3 . numbers')) . (dropFirst 10 <$> lines)
adjustToListIndices :: (Int, Int, Int) -> (Int, Int, Int)
adjustToListIndices (times, from, to) = (times, (from - 1), (to - 1))
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]
| x == to -> prepend (head (l !! from)) (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
move1 :: (Int, Int, Int) -> [String] -> [String]
move1 (times, from, to) l = map (\x -> if
| x == from -> dropFirst times (l !! from)
| x == to -> take times (l !! from) ++ (l !! to)
| otherwise -> (l !! x)) [0..length l - 1]
moves :: [(Int, Int, Int)] -> [String] -> [String]
moves [x] l = nMove x l
moves (x:xs) l = moves xs (nMove x l)
solution1 :: [String] -> String
solution1 = map (head)
moves1 :: [(Int, Int, Int)] -> [String] -> [String]
moves1 [x] l = move1 x l
moves1 (x:xs) l = moves1 xs (move1 x l)
prepend :: a -> [a] -> [a]
prepend a xs = a : xs
fib :: Int -> Int
fib 0 = 1
fib n = n * (n - 1)
part1 :: IO ()
part1 = pure (moves) <*> parseMoves <*> parseStack >>= print . map head
-- 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)))
part2 :: IO ()
part2 = pure (moves1) <*> parseMoves <*> parseStack >>= print . map head
main :: IO ()
main = pure (moves) <*> parseMoves 10 <*> parseStack 8 >>= print
main = do
part1
part2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment