data Rule = Letter Char
- -- | Then2 Rule Rule
- -- | Then3 Rule Rule Rule
- | Then [Rule]
+ | Then [Rule]
| Or Rule Rule
| See Int
deriving (Show, Eq)
let (rules, messages) = parse inputP text
print $ part1 rules messages
print $ part2 rules messages
- -- print $ part2 text
setup fname =
do text <- readFile fname
-- Parse the input
+inputP = (,) <$> rulesP <* blankLines <*> messagesP
rulesP = M.fromList <$> ruleP `sepBy` endOfLine
ruleP = (,) <$> decimal <* (string ": ") <*> ruleBodyP
thenRuleP = Then <$> seeRuleP `sepBy` (string " ")
seeRuleP = See <$> decimal
-
-inputP = (,) <$> rulesP <* blankLines <*> messagesP
-
messagesP = (munch1 isAlpha) `sepBy` endOfLine
blankLines = skipMany1 endOfLine
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
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)
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
return (myParser, updatedRules, messagesT)
-
part1 = countMatches
part2 rules messages = countMatches updatedRules 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)