AoC day 6: Custom Customs


2020-12-06T13:09:09+01:00
advent of code aoc2020 haskell

Today’s puzzle is to perform some queries on a database of survey results. This post is literate Haskell.1

import Data.List.Split
import Data.List
main = do

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.

  gs <- map lines . splitOn "\n\n" <$> readFile "day06.in"

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!


  1. For consistency. For a three-liner that could condense to one, it’s not buying much.↩︎