DEV Community

loading...

Discussion on: Advent of Code 2020 Solution Megathread - Day 6: Custom Customs

Collapse
erikbackman profile image
Erik Bäckman • Edited

Haskell:

module Main where

import Control.Arrow (Arrow((&&&)))
import Data.List.Split (splitOn)
import Control.Monad (join)
import Data.List (nub, intersect)

parseInput :: String -> [[String]]
parseInput = fmap lines . splitOn "\n\n"

solveP1 :: [[String]] -> Int
solveP1 = sum . fmap (length . nub . join)

solveP2 :: [[String]] -> Int
solveP2 = sum . fmap answers
  where
    answers [x]    = length x
    answers (x:xs) = length . foldr intersect x $ xs

main :: IO ()
main = print . (solveP1 &&& solveP2) . parseInput =<< readFile "./day6inp.txt"
Enter fullscreen mode Exit fullscreen mode