work in progress
[advent-of-code-22.git] / advent11 / Main.hs
diff --git a/advent11/Main.hs b/advent11/Main.hs
new file mode 100644 (file)
index 0000000..98e80f4
--- /dev/null
@@ -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