Advent of Code solutions https://adventofcode.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
777B

  1. import Data.List
  2. main :: IO ()
  3. main = do
  4. input <- readFile "input"
  5. print . sum . map length . groups $ input
  6. -- Collect each group's answers
  7. groups :: String -> [String]
  8. groups = map (foldr1 intersect . lines) . dlfsplit
  9. -- Split a string into a list across all the double newlines in it
  10. dlfsplit :: String -> [String]
  11. dlfsplit str = a : case b of
  12. [] -> []
  13. b -> dlfsplit b
  14. where (a,b) = dlfsplitonce str
  15. -- Split a string into two across the first double newline found
  16. dlfsplitonce :: String -> (String, String)
  17. dlfsplitonce [] = ([], [])
  18. dlfsplitonce (ca:cb:rest)
  19. | ca == '\n' && cb == '\n' = ([], rest)
  20. | otherwise = (ca:before, after)
  21. where (before, after) = dlfsplitonce (cb : rest)
  22. dlfsplitonce c = (c, [])