From ee33ca141d4dcd7d07e5c35be1e670b728a1483d Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 14 Dec 2022 10:50:41 +0000 Subject: [PATCH] work in progress --- advent-of-code22.cabal | 5 +++ advent11/Main.hs | 75 ++++++++++++++++++++++++++++++++++++++++++ data/advent11.txt | 55 +++++++++++++++++++++++++++++++ data/advent11a.txt | 27 +++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 advent11/Main.hs create mode 100644 data/advent11.txt create mode 100644 data/advent11a.txt diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index 1810d15..cb1bf56 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -150,3 +150,8 @@ executable advent10 import: common-extensions, build-directives main-is: advent10/Main.hs build-depends: text, attoparsec, split + +executable advent11 + import: common-extensions, build-directives + main-is: advent11/Main.hs + build-depends: text, attoparsec, containers, lens diff --git a/advent11/Main.hs b/advent11/Main.hs new file mode 100644 index 0000000..98e80f4 --- /dev/null +++ b/advent11/Main.hs @@ -0,0 +1,75 @@ +-- Writeup at https://work.njae.me.uk/2022/12/10/advent-of-code-2022-day-9/ + +import AoC +import Data.Text (Text) +import qualified Data.Text.IO as TIO +import Data.Attoparsec.Text hiding (take, D) +import Control.Applicative +import Data.List +import qualified Data.IntMap as M +import Control.Lens + +data Expression = Expression Operation Operand + deriving (Show, Eq) +data Operation = Plus | Times deriving (Show, Eq) +data Operand = Literal Int | Old deriving (Show, Eq) + +data MonkeyCode = MonkeyCode + { _operation :: Expression + , _test :: Int + , _trueTarget :: Int + , _falseTarget :: Int + } + deriving (Show, Eq) +makeLenses ''MonkeyCode + +type MoneyDescription = M.IntMap MonkeyCode +type MonkeyHolds = M.IntMap [Int] + + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- TIO.readFile dataFileName + let monkeys = successfulParse text + print monkeys + -- let steps = expandPath path + -- print $ part1 steps + -- print $ part2 steps + +-- part1, part2 :: Path -> Int +-- part1 steps = S.size $ rope ^. trace +-- where rope = ropeSteps (newRope 1) steps + + +-- Parse the input file + +-- pathP :: Parser [Direction] +-- directionP, upP, leftP, downP, rightP :: Parser Direction + +monkeysP = makeMonkeyMaps <$> (monkeyP `sepBy` (endOfLine <* endOfLine)) + where makeMonkeyMaps monkeys = + ( M.fromList $ map fst monkeys + , M.fromList $ map snd monkeys + ) + +monkeyP = mkMonkeyPair <$> mIdP <*> startingP <*> operationP <*> testP <*> trueTargetP <*> falseTargetP + where mkMonkeyPair mId holding _operation _test _trueTarget _falseTarget = + ((mId, MonkeyCode{..}), (mId, holding)) + +mIdP = ("Monkey " *> decimal) <* ":" <* endOfLine +startingP = (" Starting items: " *> (decimal `sepBy` ", ")) <* endOfLine +operationP = (" Operation: new = old " *> expressionP) <* endOfLine +testP = (" Test: divisible by " *> decimal) <* endOfLine +trueTargetP = (" If true: throw to monkey " *> decimal) <* endOfLine +falseTargetP = (" If false: throw to monkey " *> decimal) + +expressionP = Expression <$> (operatorP <* " ") <*> operandP +operatorP = (Plus <$ "+") <|> (Times <$ "*") +operandP = (Literal <$> decimal) <|> (Old <$ "old") + +successfulParse :: Text -> (MoneyDescription, MonkeyHolds) +successfulParse input = + case parseOnly monkeysP input of + Left _err -> (M.empty, M.empty) -- TIO.putStr $ T.pack $ parseErrorPretty err + Right monkeys -> monkeys diff --git a/data/advent11.txt b/data/advent11.txt new file mode 100644 index 0000000..738c0e7 --- /dev/null +++ b/data/advent11.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 83, 62, 93 + Operation: new = old * 17 + Test: divisible by 2 + If true: throw to monkey 1 + If false: throw to monkey 6 + +Monkey 1: + Starting items: 90, 55 + Operation: new = old + 1 + Test: divisible by 17 + If true: throw to monkey 6 + If false: throw to monkey 3 + +Monkey 2: + Starting items: 91, 78, 80, 97, 79, 88 + Operation: new = old + 3 + Test: divisible by 19 + If true: throw to monkey 7 + If false: throw to monkey 5 + +Monkey 3: + Starting items: 64, 80, 83, 89, 59 + Operation: new = old + 5 + Test: divisible by 3 + If true: throw to monkey 7 + If false: throw to monkey 2 + +Monkey 4: + Starting items: 98, 92, 99, 51 + Operation: new = old * old + Test: divisible by 5 + If true: throw to monkey 0 + If false: throw to monkey 1 + +Monkey 5: + Starting items: 68, 57, 95, 85, 98, 75, 98, 75 + Operation: new = old + 2 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 0 + +Monkey 6: + Starting items: 74 + Operation: new = old + 4 + Test: divisible by 7 + If true: throw to monkey 3 + If false: throw to monkey 2 + +Monkey 7: + Starting items: 68, 64, 60, 68, 87, 80, 82 + Operation: new = old * 19 + Test: divisible by 11 + If true: throw to monkey 4 + If false: throw to monkey 5 \ No newline at end of file diff --git a/data/advent11a.txt b/data/advent11a.txt new file mode 100644 index 0000000..c04eddb --- /dev/null +++ b/data/advent11a.txt @@ -0,0 +1,27 @@ +Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 79, 60, 97 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 74 + Operation: new = old + 3 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1 \ No newline at end of file -- 2.34.1