-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path19b.hs
30 lines (25 loc) · 1.02 KB
/
19b.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import AOC
import qualified Data.Map as M
main = interactg f
data Rule = Letter Char | And Rule Rule | Or Rule Rule | See Int deriving (Show, Eq, Read, Ord)
rulep :: String -> (Int, Rule)
rulep xs = (read $ init n, rs)
where
Right rs = parse rulep' $ unwords xs'
(n:xs') = words xs
rulep' = buildExpressionParser table term
term = ((See <$> integer) <|> char '"' *> (Letter <$> anyChar) <* char '"') <* spaces
table = [[Infix (spaces >> return And) AssocLeft], [Infix (char '|' >> spaces >> return Or) AssocLeft]]
mkParser :: M.Map Int Rule -> Rule -> Parser ()
mkParser _ (Letter c) = void $ char c
mkParser m (And x y) = mkParser m x >> mkParser m y
mkParser m (Or x y) = try (mkParser m x) <|> mkParser m y
mkParser m (See x) = mkParser m (m M.! x)
f [rs, ss] = count True $ map check ss
where
m = M.fromList $ map rulep rs
p = do
r42 <- many1 $ try $ mkParser m $ See 42
r31 <- many1 $ mkParser m $ See 31
guard $ length r42 > length r31
check s = isRight $ parse (p >> eof) s