1 -- Writeup at https://work.njae.me.uk/2022/12/10/advent-of-code-2022-day-9/
4 import Data.Text (Text)
5 import qualified Data.Text.IO as TIO
6 import Data.Attoparsec.Text hiding (take, D)
7 import Control.Applicative
9 import qualified Data.IntMap as M
12 data Expression = Expression Operation Operand
14 data Operation = Plus | Times deriving (Show, Eq)
15 data Operand = Literal Int | Old deriving (Show, Eq)
17 data MonkeyCode = MonkeyCode
18 { _operation :: Expression
24 makeLenses ''MonkeyCode
26 type MoneyDescription = M.IntMap MonkeyCode
27 type MonkeyHolds = M.IntMap [Int]
32 do dataFileName <- getDataFileName
33 text <- TIO.readFile dataFileName
34 let monkeys = successfulParse text
36 -- let steps = expandPath path
37 -- print $ part1 steps
38 -- print $ part2 steps
40 -- part1, part2 :: Path -> Int
41 -- part1 steps = S.size $ rope ^. trace
42 -- where rope = ropeSteps (newRope 1) steps
45 -- Parse the input file
47 -- pathP :: Parser [Direction]
48 -- directionP, upP, leftP, downP, rightP :: Parser Direction
50 monkeysP = makeMonkeyMaps <$> (monkeyP `sepBy` (endOfLine <* endOfLine))
51 where makeMonkeyMaps monkeys =
52 ( M.fromList $ map fst monkeys
53 , M.fromList $ map snd monkeys
56 monkeyP = mkMonkeyPair <$> mIdP <*> startingP <*> operationP <*> testP <*> trueTargetP <*> falseTargetP
57 where mkMonkeyPair mId holding _operation _test _trueTarget _falseTarget =
58 ((mId, MonkeyCode{..}), (mId, holding))
60 mIdP = ("Monkey " *> decimal) <* ":" <* endOfLine
61 startingP = (" Starting items: " *> (decimal `sepBy` ", ")) <* endOfLine
62 operationP = (" Operation: new = old " *> expressionP) <* endOfLine
63 testP = (" Test: divisible by " *> decimal) <* endOfLine
64 trueTargetP = (" If true: throw to monkey " *> decimal) <* endOfLine
65 falseTargetP = (" If false: throw to monkey " *> decimal)
67 expressionP = Expression <$> (operatorP <* " ") <*> operandP
68 operatorP = (Plus <$ "+") <|> (Times <$ "*")
69 operandP = (Literal <$> decimal) <|> (Old <$ "old")
71 successfulParse :: Text -> (MoneyDescription, MonkeyHolds)
72 successfulParse input =
73 case parseOnly monkeysP input of
74 Left _err -> (M.empty, M.empty) -- TIO.putStr $ T.pack $ parseErrorPretty err
75 Right monkeys -> monkeys