From: Neil Smith Date: Tue, 29 Dec 2020 14:30:30 +0000 (+0000) Subject: A bit of tidying, a bit of investigation X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=ffd4e09b86e7edb1694e9a88feaa67b4abef4890;p=advent-of-code-20.git A bit of tidying, a bit of investigation --- diff --git a/advent19/src/advent19.hs b/advent19/src/advent19.hs index 01e872d..a1fd6cc 100644 --- a/advent19/src/advent19.hs +++ b/advent19/src/advent19.hs @@ -12,9 +12,7 @@ import Data.Either data Rule = Letter Char - -- | Then2 Rule Rule - -- | Then3 Rule Rule Rule - | Then [Rule] + | Then [Rule] | Or Rule Rule | See Int deriving (Show, Eq) @@ -29,7 +27,6 @@ main = let (rules, messages) = parse inputP text print $ part1 rules messages print $ part2 rules messages - -- print $ part2 text setup fname = do text <- readFile fname @@ -66,6 +63,7 @@ makeParser m (See i) = makeParser m (m!i) -- Parse the input +inputP = (,) <$> rulesP <* blankLines <*> messagesP rulesP = M.fromList <$> ruleP `sepBy` endOfLine ruleP = (,) <$> decimal <* (string ": ") <*> ruleBodyP @@ -76,9 +74,6 @@ orRuleP = Or <$> thenRuleP <* (string " | ") <*> thenRuleP thenRuleP = Then <$> seeRuleP `sepBy` (string " ") seeRuleP = See <$> decimal - -inputP = (,) <$> rulesP <* blankLines <*> messagesP - messagesP = (munch1 isAlpha) `sepBy` endOfLine blankLines = skipMany1 endOfLine diff --git a/advent19/src/advent19atto.hs b/advent19/src/advent19atto.hs index b322324..6210406 100644 --- a/advent19/src/advent19atto.hs +++ b/advent19/src/advent19atto.hs @@ -17,33 +17,26 @@ import Data.Either data Rule = Letter Char - -- | Then2 Rule Rule - -- | Then3 Rule Rule Rule | Then [Rule] | Or Rule Rule | See Int deriving (Show, Eq) --- data Tree = TEmpty --- | TLetter Char --- | TThen [Tree] --- deriving (Show, Eq) - type RuleSet = M.IntMap Rule main :: IO () main = - do text <- TIO.readFile "data/advent19b.txt" + do text <- TIO.readFile "data/advent19.txt" -- print text let (rules, messages) = successfulParse inputP text let messagesT = map T.pack messages - -- print rules - -- print messages + -- TIO.writeFile "rules19.atto.txt" $ T.pack $ show rules + print $ length rules + print $ length messages print $ part1 rules messagesT print $ part2 rules messagesT - -- print $ part2 text setup fname = do text <- TIO.readFile fname @@ -73,8 +66,8 @@ countMatches rules messages makeParser :: RuleSet -> Rule -> Parser () makeParser m (Letter c) = void $ char c -makeParser m (Then rs) = mapM_ (makeParser m) rs -makeParser m (Or a b) = (makeParser m a) <|> (makeParser m b) +makeParser m (Then rs) = mapM_ (\r -> try (makeParser m r)) rs +makeParser m (Or a b) = (try (makeParser m a)) <|> (makeParser m b) makeParser m (See i) = makeParser m (m!i) diff --git a/advent19/src/advent19mega.hs b/advent19/src/advent19mega.hs index 073d6cf..2ff5e3c 100644 --- a/advent19/src/advent19mega.hs +++ b/advent19/src/advent19mega.hs @@ -29,15 +29,14 @@ type RuleSet = M.IntMap Rule main :: IO () main = - do text <- TIO.readFile "data/advent19b.txt" - -- print text + do text <- TIO.readFile "data/advent19.txt" let (rules, messages) = successfulParse text let messagesT = map T.pack messages - -- print rules - -- print messages + -- TIO.writeFile "rules19.mega.txt" $ T.pack $ show rules + print $ length rules + print $ length messages print $ part1 rules messagesT print $ part2 rules messagesT - -- print $ part2 text setup fname = do text <- TIO.readFile fname @@ -49,7 +48,6 @@ setup fname = return (myParser, updatedRules, messagesT) - part1 = countMatches part2 rules messages = countMatches updatedRules messages @@ -60,18 +58,17 @@ countMatches rules messages = length $ filter isRight $ map (parse myParser "message") messages - where myParser = (makeParser rules (See 0)) -- <* eof + where myParser = (makeParser rules (See 0)) <* eof prettyResults rs = map p rs where p (Left e) = errorBundlePretty e p (Right r) = "^" ++ show r - -- Generate the rules makeParser :: RuleSet -> Rule -> Parser () makeParser m (Letter c) = void $ char c -makeParser m (Then rs) = mapM_ (makeParser m) rs +makeParser m (Then rs) = mapM_ (\r -> try (makeParser m r)) rs makeParser m (Or a b) = (try (makeParser m a)) <|> (makeParser m b) makeParser m (See i) = makeParser m (m!i)