Commit ffd4e09b authored by Neil Smith's avatar Neil Smith
Browse files

A bit of tidying, a bit of investigation

parent cc623989
......@@ -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
......
......@@ -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)
......
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment