2 import Control.Applicative ((<$), (<*), (*>), liftA)
3 import Data.List (partition, union, intersect)
5 data Chunk = Include String | Exclude String deriving (Show)
6 data ChunkV = Includev Bool | Excludev Bool deriving (Show)
8 chunkValue :: Chunk -> String
9 chunkValue (Include v) = v
10 chunkValue (Exclude v) = v
12 isInclude :: Chunk -> Bool
13 isInclude (Include _) = True
14 isInclude (Exclude _) = False
16 chunkValueV :: ChunkV -> Bool
17 chunkValueV (Includev v) = v
18 chunkValueV (Excludev v) = v
20 isIncludeV :: ChunkV -> Bool
21 isIncludeV (Includev _) = True
22 isIncludeV (Excludev _) = False
27 text <- readFile "advent07.txt"
32 part1 :: String -> IO ()
34 print $ length $ filter (allowsAbba) $ successfulParse $ parseI7vf text
37 part2 :: String -> IO ()
39 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
47 i7file = i7line `endBy` newline
48 i7line = many1 (includeChunk <|> excludeChunk)
50 chunk = many1 alphaNum
52 excludeChunk = Exclude <$> (between (char '[') (char ']') $ chunk)
53 includeChunk = Include <$> chunk
55 hasABBA = preambleAbba <* (many alphaNum)
56 preambleAbba = (try abba) <|> (alphaNum >> preambleAbba)
67 i7filev = i7linev `endBy` newline
68 i7linev = many1 (includeChunkv <|> excludeChunkv)
70 excludeChunkv = Excludev <$> (between (char '[') (char ']') $ hasABBAv)
71 includeChunkv = Includev <$> hasABBAv
74 (try (id True <$ preambleAbba <* (many alphaNum)))
76 (id False <$ (many1 alphaNum))
79 parseI7f :: String -> Either ParseError [[Chunk]]
80 parseI7f input = parse i7file "(unknown)" input
82 parseI7 :: String -> Either ParseError [Chunk]
83 parseI7 input = parse i7line "(unknown)" input
85 parseAbba :: String -> Either ParseError String
86 parseAbba input = parse hasABBA "(unknown)" input
88 parseI7v :: String -> Either ParseError [ChunkV]
89 parseI7v input = parse i7linev "(unknown)" input
91 parseI7vf :: String -> Either ParseError [[ChunkV]]
92 parseI7vf input = parse i7filev "(unknown)" input
94 successfulParse :: Either ParseError [a] -> [a]
95 successfulParse (Left _) = []
96 successfulParse (Right a) = a
99 allSubstrings :: Int -> [a] -> [[a]]
102 | otherwise = (take n es) : (allSubstrings n $ tail es)
104 ieCandidates :: [Chunk] -> ([String], [String])
105 ieCandidates chunks = (includeCandidates, excludeCandidates)
106 where (includeChunks, excludeChunks) = partition (isInclude) chunks
107 isABA s = (s!!0 == s!!2) && (s!!0 /= s!!1)
108 candidates = (filter (isABA)) . (foldl (union) []) . (map ((allSubstrings 3) . chunkValue))
109 includeCandidates = candidates includeChunks
110 excludeCandidates = candidates excludeChunks
112 inverseABA :: String -> String
113 inverseABA s = [s!!1, s!!0, s!!1]
115 supportsSSL :: [Chunk] -> Bool
116 supportsSSL chunks = not $ null $ intersect abas eabas
117 where (abas, babs) = ieCandidates chunks
118 eabas = map (inverseABA) babs