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.

30 lines
770 B

  1. module Main where
  2. import Data.List (transpose)
  3. import Data.Bits (shift, complement, (.&.))
  4. main :: IO ()
  5. main = interact solution
  6. solution :: String -> String
  7. solution = show
  8. . (\x -> x * (complement x .&. 4095))
  9. . bits_to_number
  10. . map mostcommon
  11. . transpose
  12. . map (map ((read :: String -> Int) . (pure :: a -> [a])))
  13. . lines
  14. where
  15. mostcommon :: [Int] -> Int
  16. mostcommon a = if count 0 a > count 1 a then 0 else 1;
  17. count :: Eq a => a -> [a] -> Int
  18. count e [] = 0
  19. count e (a:r) = count e r + if a == e then 1 else 0
  20. bits_to_number :: [Int] -> Int
  21. bits_to_number = btn' . reverse
  22. where
  23. btn' :: [Int] -> Int
  24. btn' [] = 0
  25. btn' (a:r) = shift (btn' r) 1 + a