X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent07.hs;h=2705b9d161a0c3652371fd2b9c8182cba99081a4;hb=42fed81078c6babc676774a0c2816d49209f7953;hp=08bac0ef7bae515e02bfdbd17a6c449af2e105ce;hpb=8c37e8bbcbc7271940d97deb8b18a338d2970f8a;p=advent-of-code-16.git diff --git a/advent07.hs b/advent07.hs index 08bac0e..2705b9d 100644 --- a/advent07.hs +++ b/advent07.hs @@ -1,6 +1,7 @@ import Text.Parsec import Control.Applicative ((<$), (<*), (*>), liftA) -import Data.List (partition, union, intersect) +import Data.List (partition, union, intersect, tails) +import Data.Char (isAlphaNum) data Chunk = Include String | Exclude String deriving (Show) data ChunkV = Includev Bool | Excludev Bool deriving (Show) @@ -38,12 +39,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 +54,30 @@ 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] + +-- where +--   firstChar = satisfy (\a -> isLetter a || a == '_') +--   nonFirstChar = satisfy (\a -> isDigit a || isLetter a || a == '_') + + -- b <- bChar +-- where bChar = satisfy (\l -> lsLetter l && l /= a) + + i7filev = i7linev `endBy` newline i7linev = many1 (includeChunkv <|> excludeChunkv) @@ -97,9 +112,11 @@ successfulParse (Right a) = a allSubstrings :: Int -> [a] -> [[a]] -allSubstrings n es - | length es < n = [] - | otherwise = (take n es) : (allSubstrings n $ tail es) +-- allSubstrings n es +-- | length es < n = [] +-- | otherwise = (take n es) : (allSubstrings n $ tail es) +allSubstrings n e = filter (\s -> length s == n) $ map (take n) $ tails e + ieCandidates :: [Chunk] -> ([String], [String]) ieCandidates chunks = (includeCandidates, excludeCandidates)