-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path19a.hs
27 lines (22 loc) · 930 Bytes
/
19a.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
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 = mkParser m (See 0)
check s = isRight $ parse (p >> eof) s