2 import Control.Applicative ((<$), (<*), (*>), liftA)
3 import Data.List (partition, union, intersect, tails)
4 import Data.Char (isAlphaNum)
6 data Chunk = Include String | Exclude String deriving (Show)
7 data ChunkV = Includev Bool | Excludev Bool deriving (Show)
9 chunkValue :: Chunk -> String
10 chunkValue (Include v) = v
11 chunkValue (Exclude v) = v
13 isInclude :: Chunk -> Bool
14 isInclude (Include _) = True
15 isInclude (Exclude _) = False
17 chunkValueV :: ChunkV -> Bool
18 chunkValueV (Includev v) = v
19 chunkValueV (Excludev v) = v
21 isIncludeV :: ChunkV -> Bool
22 isIncludeV (Includev _) = True
23 isIncludeV (Excludev _) = False
28 text <- readFile "advent07.txt"
33 part1 :: String -> IO ()
35 print $ length $ filter (allowsAbba) $ successfulParse $ parseI7vf text
38 part2 :: String -> IO ()
40 print $ length $ filter (supportsSSL) $ successfulParse $ parseI7f text
42 allowsAbba :: [ChunkV] -> Bool
43 allowsAbba chunks = (any (chunkValueV) includeChunks) && (not (any (chunkValueV) excludeChunks))
44 where (includeChunks, excludeChunks) = partition (isIncludeV) chunks
46 i7file = i7line `endBy` newline
47 i7line = many1 (includeChunk <|> excludeChunk)
49 chunk = many1 alphaNum
51 excludeChunk = Exclude <$> (between (char '[') (char ']') $ chunk)
52 includeChunk = Include <$> chunk
54 hasABBA = preambleAbba <* (many alphaNum)
55 preambleAbba = (try abba) <|> (alphaNum >> preambleAbba)
64 -- return [a, b, b, a]
74 -- firstChar = satisfy (\a -> isLetter a || a == '_')
75 -- nonFirstChar = satisfy (\a -> isDigit a || isLetter a || a == '_')
78 -- where bChar = satisfy (\l -> lsLetter l && l /= a)
82 i7filev = i7linev `endBy` newline
83 i7linev = many1 (includeChunkv <|> excludeChunkv)
85 excludeChunkv = Excludev <$> (between (char '[') (char ']') $ hasABBAv)
86 includeChunkv = Includev <$> hasABBAv
89 (try (id True <$ preambleAbba <* (many alphaNum)))
91 (id False <$ (many1 alphaNum))
94 parseI7f :: String -> Either ParseError [[Chunk]]
95 parseI7f input = parse i7file "(unknown)" input
97 parseI7 :: String -> Either ParseError [Chunk]
98 parseI7 input = parse i7line "(unknown)" input
100 parseAbba :: String -> Either ParseError String
101 parseAbba input = parse hasABBA "(unknown)" input
103 parseI7v :: String -> Either ParseError [ChunkV]
104 parseI7v input = parse i7linev "(unknown)" input
106 parseI7vf :: String -> Either ParseError [[ChunkV]]
107 parseI7vf input = parse i7filev "(unknown)" input
109 successfulParse :: Either ParseError [a] -> [a]
110 successfulParse (Left _) = []
111 successfulParse (Right a) = a
114 allSubstrings :: Int -> [a] -> [[a]]
115 -- allSubstrings n es
116 -- | length es < n = []
117 -- | otherwise = (take n es) : (allSubstrings n $ tail es)
118 allSubstrings n e = filter (\s -> length s == n) $ map (take n) $ tails e
121 ieCandidates :: [Chunk] -> ([String], [String])
122 ieCandidates chunks = (includeCandidates, excludeCandidates)
123 where (includeChunks, excludeChunks) = partition (isInclude) chunks
124 isABA s = (s!!0 == s!!2) && (s!!0 /= s!!1)
125 candidates = (filter (isABA)) . (foldl (union) []) . (map ((allSubstrings 3) . chunkValue))
126 includeCandidates = candidates includeChunks
127 excludeCandidates = candidates excludeChunks
129 inverseABA :: String -> String
130 inverseABA s = [s!!1, s!!0, s!!1]
132 supportsSSL :: [Chunk] -> Bool
133 supportsSSL chunks = not $ null $ intersect abas eabas
134 where (abas, babs) = ieCandidates chunks
135 eabas = map (inverseABA) babs