Guest User

Untitled

a guest
Dec 9th, 2019
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.IO
  2.  
  3. type SpaceImage = String
  4.  
  5. type Size = (Int, Int)
  6. totalSize size = fst size * snd size
  7.  
  8. count :: Eq a => a -> [a] -> Int
  9. count x = length . filter (x==)
  10.  
  11. data Layer = Layer { getData :: [Int] } deriving (Show)
  12.  
  13. instance Eq Layer where
  14. (Layer a) == (Layer b) = count 0 a == count 0 b
  15.  
  16. instance Ord Layer where
  17. compare (Layer a) (Layer b) = count 0 a `compare` count 0 b
  18.  
  19. identifyLayer :: Layer -> Int
  20. identifyLayer (Layer l) = count 1 l * count 2 l
  21.  
  22. layerFits :: Size -> SpaceImage -> Bool
  23. layerFits size img = length img `div` totalSize size > 0
  24.  
  25. parseLayer :: Size -> SpaceImage -> Layer
  26. parseLayer size img = Layer $ map (read . pure :: Char -> Int) $ take (totalSize size) img
  27.  
  28. parseLayers :: Size -> SpaceImage -> [Layer]
  29. parseLayers size img
  30. | layerFits size img = do let layer = parseLayer size img
  31. [layer] ++ parseLayers size (drop (length $ getData layer) img)
  32. | otherwise = []
  33.  
  34. main = do
  35. contents <- readFile "input8"
  36. print $ identifyLayer $ minimum $ parseLayers (25, 6) contents
RAW Paste Data