3 import Data.Text (Text)
4 import qualified Data.Text as T
5 import qualified Data.Text.IO as TIO
7 import Data.Attoparsec.Text
8 -- import Data.Attoparsec.Combinator
10 import qualified Data.Set as S
14 do text <- TIO.readFile "data/advent06.txt"
15 let groups = successfulParse text
20 part1 groups = sum $ map (S.size . S.unions) groups
21 part2 groups = sum $ map (S.size . intersections) groups
23 intersections :: Ord a => [S.Set a] -> S.Set a
24 intersections group = foldl S.intersection (head group) group
26 -- -- Parse the input file
28 blankLines = skipMany1 endOfLine
30 personP = S.fromList <$> many1 letter
31 groupP = sepBy1 personP endOfLine
33 groupsP = sepBy groupP blankLines
35 successfulParse :: Text -> [[S.Set Char]]
36 successfulParse input =
37 case parseOnly groupsP input of
38 Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
39 Right groups -> groups