diff options
Diffstat (limited to 'day05/app')
| -rw-r--r-- | day05/app/Main.hs | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/day05/app/Main.hs b/day05/app/Main.hs index b56e45b..f22ed5a 100644 --- a/day05/app/Main.hs +++ b/day05/app/Main.hs @@ -145,6 +145,8 @@ module Main (main) where import Data.ByteString.Lazy (ByteString) import Data.Char (digitToInt) +import Data.List (filter) +import Data.Sequence (fromList, index, splitAt, update, (><)) import Options.Applicative (Parser, ParserInfo, argument, execParser, fullDesc, help, helper, info, metavar, str) import Relude hiding (ByteString, elem, empty, filter, fromList, length, null, optional, readFile, splitAt) import Text.Parsec (ParseError, parse, (<?>)) @@ -157,7 +159,7 @@ type Opts :: Type newtype Opts = Opts {_filename :: Text} deriving stock (Show) type Supplies :: Type -newtype Supplies = Supplies {_i :: [[Char]]} deriving stock (Show) +newtype Supplies = Supplies {_i :: Seq (Seq Char)} deriving stock (Show) type Order :: Type data Order = Order {_amount :: Int, _from :: Int, _to :: Int} deriving stock (Show) @@ -166,7 +168,7 @@ parseInput :: FilePath -> ByteString -> Either ParseError (Supplies, [Order]) parseInput = parse $ (,) <$> (supplies <* eol) <*> orders where supplies :: GenParser t st Supplies - supplies = Supplies <$> many1 (lineBlock <* eol) <* axis + supplies = (Supplies . fromList . map (fromList . filter (/= ' '))) . transpose <$> many1 (lineBlock <* eol) <* axis lineBlock :: GenParser t st [Char] lineBlock = block `sepBy1` space @@ -207,11 +209,38 @@ parseInput = parse $ (,) <$> (supplies <* eol) <*> orders <?> "end of line" ) -runPart1 :: (Supplies, [Order]) -> (Supplies, [Order]) -runPart1 x = x +move :: Int -> Int -> Int -> Supplies -> Supplies +move f t n s = Supplies $ update tp1 (fst moveElements >< toStack) (update fp1 (snd moveElements) (_i s)) + where + moveElements :: (Seq Char, Seq Char) + moveElements = splitAt n fromStack + + fromStack :: Seq Char + fromStack = index (_i s) fp1 + + toStack :: Seq Char + toStack = index (_i s) tp1 + + tp1 :: Int + tp1 = t - 1 + + fp1 :: Int + fp1 = f - 1 -runPart2 :: (Supplies, [Order]) -> Int -runPart2 = undefined +runPart1 :: (Supplies, [Order]) -> [Char] +runPart1 (sup, ors) = toList $ fmap (`index` 0) (_i run) + where + run :: Supplies + run = foldl' (\s o -> loop (_amount o) (move (_from o) (_to o) 1) s) sup ors + + loop :: Int -> (Supplies -> Supplies) -> Supplies -> Supplies + loop n f s = foldl' (\s' _ -> f s') s [1 .. n] + +runPart2 :: (Supplies, [Order]) -> [Char] +runPart2 (sup, ors) = toList $ fmap (`index` 0) (_i run) + where + run :: Supplies + run = foldl' (\s o -> move (_from o) (_to o) (_amount o) s) sup ors main :: IO () main = do |
