diff --git a/2022/src/Aoc/Day5.hs b/2022/src/Aoc/Day5.hs index 68b2131..1495c68 100644 --- a/2022/src/Aoc/Day5.hs +++ b/2022/src/Aoc/Day5.hs @@ -2,9 +2,8 @@ module Aoc.Day5 (solve) where import Aoc (runReadP) import Data.Char (isDigit) -import Data.Maybe import Data.List (transpose) -import Data.IntMap.Strict (IntMap) +import Data.IntMap.Strict (IntMap, (!)) import qualified Data.IntMap.Strict as IntMap import Text.ParserCombinators.ReadP @@ -53,12 +52,18 @@ parseInput input = Input { state = state, procedure = procedure } procedureContents = drop 1 $ dropWhile (/= "") $ lines input procedure = map parseMove procedureContents -doMove :: Move -> State -> State -doMove m s = s' +doMove :: State -> Move -> State +doMove stacks (Move n x y) = IntMap.insert x newX (IntMap.insert y newY stacks) where - (forTo, backToFrom) = splitAt (amount m) $ fromJust $ IntMap.lookup (from m) s - addToTo to' = Just $ (reverse forTo) ++ to' - s' = IntMap.update addToTo (to m) $ IntMap.insert (from m) backToFrom s + (toPush, newX) = splitAt n (stacks ! x) + newY = reverse toPush ++ stacks ! y + +-- no reverse +doMove' :: State -> Move -> State +doMove' stacks (Move n x y) = IntMap.insert x newX (IntMap.insert y newY stacks) + where + (toPush, newX) = splitAt n (stacks ! x) + newY = toPush ++ stacks ! y stackLabel :: Stack -> String stackLabel [] = "" @@ -68,7 +73,13 @@ message :: State -> String message s = concat $ map stackLabel $ IntMap.elems s solve :: Integer -> String -> String -solve 1 input = message $ foldr doMove state' procedure' +solve 1 input = message $ foldl doMove state' procedure' + where + input' = parseInput input + state' = state input' + procedure' = procedure input' + +solve 2 input = message $ foldl doMove' state' procedure' where input' = parseInput input state' = state input' diff --git a/README.md b/README.md index 9337d6a..c2837ae 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ 1. 2019 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 -1. [2022] **8/50** 🌟 +1. [2022] **10/50** 🌟 [2015]: ./2015 [2017]: ./2017