Done day 23
[advent-of-code-20.git] / advent06 / src / advent06.hs
1 -- import Debug.Trace
2
3 import Data.Text (Text)
4 import qualified Data.Text as T
5 import qualified Data.Text.IO as TIO
6
7 import Data.Attoparsec.Text
8 -- import Data.Attoparsec.Combinator
9
10 import qualified Data.Set as S
11
12 main :: IO ()
13 main =
14 do text <- TIO.readFile "data/advent06.txt"
15 let groups = successfulParse text
16 -- print groups
17 print $ part1 groups
18 print $ part2 groups
19
20 part1 groups = sum $ map (S.size . S.unions) groups
21 part2 groups = sum $ map (S.size . intersections) groups
22
23 intersections :: Ord a => [S.Set a] -> S.Set a
24 intersections group = foldl S.intersection (head group) group
25
26 -- -- Parse the input file
27
28 blankLines = skipMany1 endOfLine
29
30 personP = S.fromList <$> many1 letter
31 groupP = sepBy1 personP endOfLine
32
33 groupsP = sepBy groupP blankLines
34
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