My solutions to Advent of Code 2021
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

36 lines
826 B

  1. module Main where
  2. main :: IO ()
  3. main = interact solution
  4. type Board = [[Int]]
  5. scnd :: (b -> c) -> (a, b) -> (a, c)
  6. scnd f (a, b) = (a, f b)
  7. frst :: (a -> c) -> (a, b) -> (c, b)
  8. frst f (a, b) = (f a, b)
  9. solution :: String -> String
  10. solution =
  11. show
  12. -- ([Int], [Boards])
  13. . scnd toBoards
  14. . scnd (map (map (read :: String -> Int)))
  15. . scnd (filter (not . null))
  16. . scnd (map words)
  17. . frst (map (read :: String -> Int))
  18. . frst splitcomma
  19. . (\x -> (head x, tail x))
  20. . lines
  21. where
  22. splitcomma :: String -> [String] -- "stole" the implementation from `words`
  23. splitcomma s = case dropWhile (==',') s of
  24. "" -> []
  25. s' -> w : splitcomma s''
  26. where
  27. (w, s'') = break (==',') s'
  28. toBoards :: [[Int]] -> [Board]
  29. toBoards [] = []
  30. toBoards (a:b:c:d:e:r) = [a,b,c,d,e]:toBoards r