DEV Community


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

flwidmer profile image

I decided to do this year in Haskell. Perhaps if it gets too crazy, I'll revert to what I know better, but until now it's been fun.

I might start a library with the groups and addAll functions, seems I'm going to use them for every puzzle...

import Data.List.Split ( splitOn )
import Data.List ( intersect, nub )

main :: IO ()
main = do
    input <- readFile "input"
    putStrLn "day6"
    print $ solve1 input
    print $ solve2 input

solve1 :: String -> Int
solve1 a = 
  let grouped = groups a
      answers = map (filter (\x -> x `elem` ['a'..'z']) . nub) grouped
  in  addAll answers

solve2 :: String -> Int
solve2 a =
  let grouped = map lines $ groups a
      intersected = map (foldr1 intersect) grouped 
  in  addAll intersected

addAll :: [[a]] -> Int 
addAll = sum . map length 

groups :: String -> [String]
groups = splitOn "\n\n"
Enter fullscreen mode Exit fullscreen mode