From: Neil Smith Date: Thu, 8 Dec 2016 10:01:38 +0000 (+0000) Subject: Better implementation of ABBA finding, from Reddit X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-16.git;a=commitdiff_plain;h=f502d0ff85e2e7504b88540ccad91549c8e42e1b Better implementation of ABBA finding, from Reddit --- diff --git a/advent07.hs b/advent07.hs index 08bac0e..a82177e 100644 --- a/advent07.hs +++ b/advent07.hs @@ -38,12 +38,10 @@ part2 :: String -> IO () part2 text = do print $ length $ filter (supportsSSL) $ successfulParse $ parseI7f text - allowsAbba :: [ChunkV] -> Bool allowsAbba chunks = (any (chunkValueV) includeChunks) && (not (any (chunkValueV) excludeChunks)) where (includeChunks, excludeChunks) = partition (isIncludeV) chunks - i7file = i7line `endBy` newline i7line = many1 (includeChunk <|> excludeChunk) @@ -55,14 +53,21 @@ includeChunk = Include <$> chunk hasABBA = preambleAbba <* (many alphaNum) preambleAbba = (try abba) <|> (alphaNum >> preambleAbba) +-- abba = +-- do a <- alphaNum +-- b <- alphaNum +-- if a == b then +-- fail "Identical" +-- else do char b +-- char a +-- return [a, b, b, a] + abba = do a <- alphaNum - b <- alphaNum - if a == b then - fail "Identical" - else do char b - char a - return [a, b, b, a] + b <- noneOf [a] + char b + char a + return [a, b, b, a] i7filev = i7linev `endBy` newline i7linev = many1 (includeChunkv <|> excludeChunkv)