X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-20.git;a=blobdiff_plain;f=advent07%2Fsrc%2Fadvent07graph.hs;fp=advent07%2Fsrc%2Fadvent07graph.hs;h=ee31c811b915bd0fdd37f35a7e1b5b5ebff30d2f;hp=0000000000000000000000000000000000000000;hb=072b9c66fc1e67fb78106ada274a6e6b0137c50f;hpb=bb87fe8d48a7debc85dcbff2c08a7d8267427a77 diff --git a/advent07/src/advent07graph.hs b/advent07/src/advent07graph.hs new file mode 100644 index 0000000..ee31c81 --- /dev/null +++ b/advent07/src/advent07graph.hs @@ -0,0 +1,97 @@ +-- import Debug.Trace + +import Data.Text (Text) +-- import qualified Data.Text as T +import qualified Data.Text.IO as TIO + +import Data.Attoparsec.Text +-- import Data.Attoparsec.Combinator +import Control.Applicative + +import qualified Data.Graph.DGraph as D +import qualified Data.Graph.Types as G +import qualified Data.Graph.Traversal as T + +import qualified Data.Set as S +import qualified Data.Map.Strict as M + +data QuantifiedBag = QuantifiedBag Int String + deriving (Show, Eq, Ord) + +type QBags = S.Set QuantifiedBag +type BagRules = M.Map String QBags +type BagGraph = D.DGraph String Int + + +main :: IO () +main = + do text <- TIO.readFile "data/advent07.txt" + let bags = successfulParse text + let graph = buildGraph bags + -- print graph + -- dumpBagDot bags + print $ part1 graph + print $ part2 graph + +part1 graph = length (T.bfsVertices (D.transpose graph) "shiny gold") - 1 + +part2 graph = (bfsCount graph "shiny gold") - 1 + +bfsCount :: BagGraph -> String -> Int +bfsCount graph thisBag = 1 + (sum $ map subCount others) + where others = D.outboundingArcs graph thisBag + subCount a = (G.attribute a) * (bfsCount graph $ G.destinationVertex a) + + +buildGraph :: BagRules -> BagGraph +buildGraph rules = M.foldrWithKey addRule G.empty rules + +addRule :: String -> QBags -> BagGraph -> BagGraph +addRule source dests graph = S.foldr (addArc source) graph dests + +addArc :: String -> QuantifiedBag -> BagGraph -> BagGraph +addArc source (QuantifiedBag quantity destination) graph = D.insertArc arc graph + where arc = G.Arc source destination quantity + + +-- dumpBagDot bags = +-- do writeFile "a07dump.dot" "digraph {\n" +-- mapM_ dumpABag (M.assocs bags) +-- appendFile "a07dump.dot" "shiny_gold [fillcolor = gold1 ]\n" +-- appendFile "a07dump.dot" "}\n" + +-- dumpABag (bag, contents) = +-- mapM_ (dumpALink bag) (S.toList contents) + +-- dumpALink bag (QuantifiedBag n name) = +-- do let name' = squashName name +-- let bag' = squashName bag +-- let txt = bag' ++ " -> " ++ name' ++ "\n" +-- appendFile "a07dump.dot" txt + +-- squashName :: String -> String +-- squashName name = [if c == ' ' then '_' else c | c <- name] + + +-- -- Parse the input file + +bagNameP = manyTill anyChar ((string " bags") <|> (string " bag")) + +quantifiedBagP = QuantifiedBag <$> decimal <* space <*> bagNameP + +emptyBagListP = "no other bags" *> pure S.empty + +bagListP = S.fromList <$> sepBy quantifiedBagP (string ", ") + +bagContentsP = emptyBagListP <|> bagListP + +ruleP = (,) <$> bagNameP <* " contain " <*> bagContentsP <* "." + +rulesP = M.fromList <$> sepBy ruleP endOfLine + + +successfulParse :: Text -> BagRules +successfulParse input = + case parseOnly rulesP input of + Left _err -> M.empty -- TIO.putStr $ T.pack $ parseErrorPretty err + Right bags -> bags