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

Done day 19, but not sure why attoparsec isn't working

parent 39598d4a
# This YAML file describes your package. Stack will automatically generate a
# Cabal file when you run `stack build`. See the hpack website for help with
# this file: <https://github.com/sol/hpack>.
name: advent19
synopsis: Advent of Code
version: '0.0.1'
default-extensions:
- AllowAmbiguousTypes
- ApplicativeDo
- BangPatterns
- BlockArguments
- DataKinds
- DeriveFoldable
- DeriveFunctor
- DeriveGeneric
- DeriveTraversable
- EmptyCase
- FlexibleContexts
- FlexibleInstances
- FunctionalDependencies
- GADTs
- GeneralizedNewtypeDeriving
- ImplicitParams
- KindSignatures
- LambdaCase
- MonadComprehensions
- MonoLocalBinds
- MultiParamTypeClasses
- MultiWayIf
- NamedFieldPuns
- NegativeLiterals
- NumDecimals
# - OverloadedLists
- OverloadedStrings
- PartialTypeSignatures
- PatternGuards
- PatternSynonyms
- PolyKinds
- RankNTypes
- RecordWildCards
- ScopedTypeVariables
- TemplateHaskell
- TransformListComp
- TupleSections
- TypeApplications
- TypeFamilies
- TypeInType
- TypeOperators
- ViewPatterns
executables:
advent19:
main: advent19.hs
source-dirs: src
dependencies:
- base >= 2 && < 6
- text
- containers
advent19atto:
main: advent19atto.hs
source-dirs: src
dependencies:
- base >= 2 && < 6
- text
- attoparsec
- containers
advent19mega:
main: advent19mega.hs
source-dirs: src
dependencies:
- base >= 2 && < 6
- text
- megaparsec
- containers
-- import Debug.Trace
import Text.ParserCombinators.ReadP
-- import Text.ParserCombinators.ReadP ((+++))
import Data.Char (isDigit, isAlpha)
import qualified Data.IntMap.Strict as M
import Data.IntMap.Strict ((!))
import Data.Functor (void)
import Data.Either
data Rule = Letter Char
-- | Then2 Rule Rule
-- | Then3 Rule Rule Rule
| Then [Rule]
| Or Rule Rule
| See Int
deriving (Show, Eq)
type RuleSet = M.IntMap Rule
main :: IO ()
main =
do text <- readFile "data/advent19.txt"
-- print text
let (rules, messages) = parse inputP text
print $ part1 rules messages
print $ part2 rules messages
-- print $ part2 text
setup fname =
do text <- readFile fname
let (rules, messages) = parse inputP text
let newRules = parse rulesP "8: 42 | 42 8\n11: 42 31 | 42 11 31"
let updatedRules = M.union newRules rules
let myParser = makeParser updatedRules (See 0)
return (myParser, updatedRules, messages)
part1 = countMatches
part2 rules messages = countMatches updatedRules messages
where newRules = parse rulesP "8: 42 | 42 8\n11: 42 31 | 42 11 31"
updatedRules = M.union newRules rules
countMatches rules messages
= length $ filter ((== "") . snd) results
where myParser = makeParser rules (See 0)
results = concatMap (readP_to_S myParser) messages
parse :: ReadP a -> String -> a
parse parser str = fst $ head $ filter ((== "") . snd) $ readP_to_S parser str
-- Generate the rules
makeParser :: RuleSet -> Rule -> ReadP ()
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 (See i) = makeParser m (m!i)
-- Parse the input
rulesP = M.fromList <$> ruleP `sepBy` endOfLine
ruleP = (,) <$> decimal <* (string ": ") <*> ruleBodyP
ruleBodyP = choice [letterRuleP, orRuleP, thenRuleP, seeRuleP]
letterRuleP = Letter <$> between (string "\"") (string "\"") get
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
decimal = read <$> many1 (satisfy isDigit)
endOfLine = char '\n'
-- 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 Control.Applicative.Combinators
import qualified Data.IntMap.Strict as M
import Data.IntMap.Strict ((!))
import Data.Functor (void)
import Prelude hiding (take)
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"
-- print text
let (rules, messages) = successfulParse inputP text
let messagesT = map T.pack messages
-- print rules
-- print messages
print $ part1 rules messagesT
print $ part2 rules messagesT
-- print $ part2 text
setup fname =
do text <- TIO.readFile fname
let (rules, messages) = successfulParse inputP text
let messagesT = map T.pack messages
let Right newRules = parseOnly rulesP "8: 42 | 42 8\n11: 42 31 | 42 11 31"
let updatedRules = M.union newRules rules
let myParser = (makeParser updatedRules (See 0)) <* endOfInput
return (myParser, updatedRules, messagesT)
part1 = countMatches
part2 rules messages = countMatches updatedRules messages
where Right newRules = parseOnly rulesP "8: 42 | 42 8\n11: 42 31 | 42 11 31"
updatedRules = M.union newRules rules
countMatches rules messages
= length
$ filter isRight
$ map (parseOnly myParser) messages
where myParser = ((makeParser rules (See 0)) <* endOfInput)
-- Generate the rules
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 (See i) = makeParser m (m!i)
-- Parse the input
rulesP = M.fromList <$> ruleP `sepBy` endOfLine
ruleP = (,) <$> decimal <* ": " <*> ruleBodyP
ruleBodyP = choice [letterRuleP, orRuleP, thenRuleP, seeRuleP]
letterRuleP = Letter <$> ("\"" *> anyChar) <* "\""
orRuleP = Or <$> thenRuleP <* " | " <*> thenRuleP
thenRuleP = Then <$> seeRuleP `sepBy` (string " ")
seeRuleP = See <$> decimal
inputP = (,) <$> rulesP <* blankLines <*> messagesP
messagesP = (many1 letter) `sepBy` endOfLine
blankLines = skipMany1 endOfLine
-- successfulParse :: Text -> (Integer, [Maybe Integer])
successfulParse parser input =
case parseOnly parser input of
Left _err -> (M.empty, []) -- TIO.putStr $ T.pack $ parseErrorPretty err
Right expressions -> expressions
-- import Debug.Trace
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import Data.Void (Void)
import Text.Megaparsec hiding (State)
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
import qualified Control.Applicative as CA
import qualified Data.IntMap.Strict as M
import Data.IntMap.Strict ((!))
import Data.Functor (void)
import Prelude hiding (take)
import Data.Either
data Rule = Letter Char
| Then [Rule]
| Or Rule Rule
| See Int
deriving (Show, Eq)
type RuleSet = M.IntMap Rule
main :: IO ()
main =
do text <- TIO.readFile "data/advent19b.txt"
-- print text
let (rules, messages) = successfulParse text
let messagesT = map T.pack messages
-- print rules
-- print messages
print $ part1 rules messagesT
print $ part2 rules messagesT
-- print $ part2 text
setup fname =
do text <- TIO.readFile fname
let (rules, messages) = successfulParse text
let messagesT = map T.pack messages
let Right newRules = parse rulesP "rules" "8: 42 | 42 8\n11: 42 31 | 42 11 31"
let updatedRules = M.union newRules rules
let myParser = (makeParser updatedRules (See 0)) -- <* eof
return (myParser, updatedRules, messagesT)
part1 = countMatches
part2 rules messages = countMatches updatedRules messages
where Right newRules = parse rulesP "rules" "8: 42 | 42 8\n11: 42 31 | 42 11 31"
updatedRules = M.union newRules rules
countMatches rules messages
= length
$ filter isRight
$ map (parse myParser "message") messages
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 (Or a b) = (try (makeParser m a)) <|> (makeParser m b)
makeParser m (See i) = makeParser m (m!i)
-- Parse the input
type Parser = Parsec Void Text
sc :: Parser ()
sc = L.space (skipSome (char ' ')) CA.empty CA.empty
lexeme = L.lexeme sc
integer = lexeme L.decimal
symb = L.symbol sc
colonP = symb ":"
pipeP = symb "|"
quoteP = symb "\""
rulesP = M.fromList <$> ruleP `sepEndBy` newline
ruleP = (,) <$> integer <* colonP <*> ruleBodyP
ruleBodyP = choice [(try letterRuleP), (try orRuleP), (try thenRuleP), (try seeRuleP)]
letterRuleP = Letter <$> between quoteP quoteP letterChar
orRuleP = Or <$> thenRuleP <* pipeP <*> thenRuleP
thenRuleP = Then <$> some seeRuleP
seeRuleP = See <$> integer
inputP = (,) <$> rulesP <* (some newline) <*> messagesP
messagesP = messageP `sepBy` newline
messageP = some letterChar
-- successfulParse :: Text -> (Integer, [Maybe Integer])
successfulParse input =
case parse inputP "input" input of
Left _err -> (M.empty, []) -- TIO.putStr $ T.pack $ parseErrorPretty err
Right expressions -> expressions
This diff is collapsed.
0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b"
ababbb
bababa
abbbab
aaabbb
aaaabbb
\ No newline at end of file
42: 9 14 | 10 1
9: 14 27 | 1 26
10: 23 14 | 28 1
1: "a"
11: 42 31
5: 1 14 | 15 1
19: 14 1 | 14 14
12: 24 14 | 19 1
16: 15 1 | 14 14
31: 14 17 | 1 13
6: 14 14 | 1 14
2: 1 24 | 14 4
0: 8 11
13: 14 3 | 1 12
15: 1 | 14
17: 14 2 | 1 7
23: 25 1 | 22 14
28: 16 1
4: 1 1
20: 14 14 | 1 15
3: 5 14 | 16 1
27: 1 6 | 14 18
14: "b"
21: 14 1 | 1 14
25: 1 1 | 1 14
22: 14 14
8: 42
26: 14 22 | 1 20
18: 15 15
7: 14 5 | 1 21
24: 14 1
abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa
bbabbbbaabaabba
babbbbaabbbbbabbbbbbaabaaabaaa
aaabbbbbbaaaabaababaabababbabaaabbababababaaa
bbbbbbbaaaabbbbaaabbabaaa
bbbababbbbaaaaaaaabbababaaababaabab
ababaaaaaabaaab
ababaaaaabbbaba
baabbaaaabbaaaababbaababb
abbbbabbbbaaaababbbbbbaaaababb
aaaaabbaabaaaaababaa
aaaabbaaaabbaaa
aaaabbaabbaaaaaaabbbabbbaaabbaabaaa
babaaabbbaaabaababbaabababaaab
aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba
\ No newline at end of file
0: 8 11
8: 42 | 42 8
42: "a"
11: 42 31 | 42 11 31
31: "b"
ab
aabb
aaa
b
aaab
\ No newline at end of file
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8"/>
<title>Day 19 - Advent of Code 2020</title>
<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
<link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'/>
<link rel="stylesheet" type="text/css" href="/static/style.css?25"/>
<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
<link rel="shortcut icon" href="/favicon.png"/>
</head><!--
Oh, hello! Funny seeing you here.
I appreciate your enthusiasm, but you aren't going to find much down here.
There certainly aren't clues to any of the puzzles. The best surprises don't
even appear in the source until you unlock them for real.
Please be careful with automated requests; I'm not a massive company, and I can
only take so much traffic. Please be considerate so that everyone gets to play.
If you're curious about how Advent of Code works, it's running on some custom
Perl code. Other than a few integrations (auth, analytics, social media), I
built the whole thing myself, including the design, animations, prose, and all
of the puzzles.
The puzzles are most of the work; preparing a new calendar and a new set of
puzzles each year takes all of my free time for 4-5 months. A lot of effort
went into building this thing - I hope you're enjoying playing it as much as I
enjoyed making it for you!
If you'd like to hang out, I'm @ericwastl on Twitter.
- Eric Wastl
-->
<body>
<header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2020/about">[About]</a></li><li><a href="/2020/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2020/settings">[Settings]</a></li><li><a href="/2020/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2020/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">38*</span></div></div><div><h1 class="title-event">&nbsp;<span class="title-event-wrap">{&apos;year&apos;:</span><a href="/2020">2020</a><span class="title-event-wrap">}</span></h1><nav><ul><li><a href="/2020">[Calendar]</a></li><li><a href="/2020/support">[AoC++]</a></li><li><a href="/2020/sponsors">[Sponsors]</a></li><li><a href="/2020/leaderboard">[Leaderboard]</a></li><li><a href="/2020/stats">[Stats]</a></li></ul></nav></div></header>
<div id="sidebar">
<div id="sponsor"><div class="quiet">Our <a href="/2020/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://jobs.americanexpress.com/technology" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">American Express</a> - We architect, code and ship software that makes us an essential part of our customers’ digital lives. Work with the latest tech and back the engineering community through open source. Find your place in tech on #TeamAmex.</div></div>
</div><!--/sidebar-->
<main>
<script>window.addEventListener('click', function(e,s,r){if(e.target.nodeName==='CODE'&&e.detail===3){s=window.getSelection();s.removeAllRanges();r=document.createRange();r.selectNodeContents(e.target);s.addRange(r);}});</script>
<article class="day-desc"><h2>--- Day 19: Monster Messages ---</h2><p>You land in an airport surrounded by dense forest. As you walk to your high-speed train, the Elves at the <span title="This is a purely fictional organization. Any resemblance to actual organizations, past or present, is purely coincidental.">Mythical Information Bureau</span> contact you again. They think their satellite has collected an image of a <em>sea monster</em>! Unfortunately, the connection to the satellite is having problems, and many of the messages sent back from the satellite have been corrupted.</p>
<p>They sent you a list of <em>the rules valid messages should obey</em> and a list of <em>received messages</em> they've collected so far (your puzzle input).</p>
<p>The <em>rules for valid messages</em> (the top part of your puzzle input) are numbered and build upon each other. For example:</p>
<pre><code>0: 1 2
1: "a"
2: 1 3 | 3 1
3: "b"
</code></pre>
<p>Some rules, like <code>3: "b"</code>, simply match a single character (in this case, <code>b</code>).</p>
<p>The remaining rules list the sub-rules that must be followed; for example, the rule <code>0: 1 2</code> means that to match rule <code>0</code>, the text being checked must match rule <code>1</code>, and the text after the part that matched rule <code>1</code> must then match rule <code>2</code>.</p>
<p>Some of the rules have multiple lists of sub-rules separated by a pipe (<code>|</code>). This means that <em>at least one</em> list of sub-rules must match. (The ones that match might be different each time the rule is encountered.) For example, the rule <code>2: 1 3 | 3 1</code> means that to match rule <code>2</code>, the text being checked must match rule <code>1</code> followed by rule <code>3</code> <em>or</em> it must match rule <code>3</code> followed by rule <code>1</code>.</p>
<p>Fortunately, there are no loops in the rules, so the list of possible matches will be finite. Since rule <code>1</code> matches <code>a</code> and rule <code>3</code> matches <code>b</code>, rule <code>2</code> matches either <code>ab</code> or <code>ba</code>. Therefore, rule <code>0</code> matches <code>aab</code> or <code>aba</code>.</p>
<p>Here's a more interesting example:</p>
<pre><code>0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b"
</code></pre>
<p>Here, because rule <code>4</code> matches <code>a</code> and rule <code>5</code> matches <code>b</code>, rule <code>2</code> matches two letters that are the same (<code>aa</code> or <code>bb</code>), and rule <code>3</code> matches two letters that are different (<code>ab</code> or <code>ba</code>).</p>
<p>Since rule <code>1</code> matches rules <code>2</code> and <code>3</code> once each in either order, it must match two pairs of letters, one pair with matching letters and one pair with different letters. This leaves eight possibilities: <code>aaab</code>, <code>aaba</code>, <code>bbab</code>, <code>bbba</code>, <code>abaa</code>, <code>abbb</code>, <code>baaa</code>, or <code>babb</code>.</p>
<p>Rule <code>0</code>, therefore, matches <code>a</code> (rule <code>4</code>), then any of the eight options from rule <code>1</code>, then <code>b</code> (rule <code>5</code>): <code>aaaabb</code>, <code>aaabab</code>, <code>abbabb</code>, <code>abbbab</code>, <code>aabaab</code>, <code>aabbbb</code>, <code>abaaab</code>, or <code>ababbb</code>.</p>
<p>The <em>received messages</em> (the bottom part of your puzzle input) need to be checked against the rules so you can determine which are valid and which are corrupted. Including the rules and the messages together, this might look like:</p>
<pre><code>0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b"
ababbb
bababa
abbbab
aaabbb
aaaabbb
</code></pre>
<p>Your goal is to determine <em>the number of messages that completely match rule <code>0</code></em>. In the above example, <code>ababbb</code> and <code>abbbab</code> match, but <code>bababa</code>, <code>aaabbb</code>, and <code>aaaabbb</code> do not, producing the answer <em><code>2</code></em>. The whole message must match all of rule <code>0</code>; there can't be extra unmatched characters in the message. (For example, <code>aaaabbb</code> might appear to match rule <code>0</code> above, but it has an extra unmatched <code>b</code> on the end.)</p>
<p><em>How many messages completely match rule <code>0</code>?</em></p>
</article>
<p>Your puzzle answer was <code>235</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>As you look over the list of messages, you realize your matching rules aren't quite right. To fix them, completely replace rules <code>8: 42</code> and <code>11: 42 31</code> with the following:</p>
<pre><code>8: 42 | 42 8
11: 42 31 | 42 11 31
</code></pre>
<p>This small change has a big impact: now, the rules <em>do</em> contain loops, and the list of messages they could hypothetically match is infinite. You'll need to determine how these changes affect which messages are valid.</p>
<p>Fortunately, many of the rules are unaffected by this change; it might help to start by looking at which rules always match the same set of values and how <em>those</em> rules (especially rules <code>42</code> and <code>31</code>) are used by the new versions of rules <code>8</code> and <code>11</code>.</p>
<p>(Remember, <em>you only need to handle the rules you have</em>; building a solution that could handle any hypothetical combination of rules would be <a href="https://en.wikipedia.org/wiki/Formal_grammar" target="_blank">significantly more difficult</a>.)</p>
<p>For example:</p>