import Data.List.Split import Data.List = domain
The data is provided as paragraphs of lines of characters, representing groups of results of questions that were answered positively.
I’ll use the old Perl trick to split on
"\n\n" to extract paragraphs, then split on lines.
<- map lines . splitOn "\n\n" <$> readFile "day06.in" gs
Part 1 is the number of questions to which someone in the group answered positively, summed across groups. In set parlance, that’s the union of the positive answers.
Data.List happens to have primitives for set operations.
print $ sum $ map (length . foldr1 union) gs
Part 2 is the number of questions to which everyone in the group answered positively, summed across groups. In set parlance, that’s the intersection of the positive answers.
print $ sum $ map (length . foldr1 intersect) gs
And that’s all there is to it. See you soon for another AoC solution!
For consistency. For a three-liner that could condense to one, it’s not buying much.↩︎