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
--- /dev/null
+-- 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
--- /dev/null
+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
--- /dev/null
+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