7c02922de66734f83bd0322c0a13b043f010d1f0
[advent-of-code-19.git] / advent08 / src / advent08.hs
1 import Data.List
2 import Data.List.Split
3 import Data.Char
4 import Data.Ord
5
6 main :: IO ()
7 main = do
8 text <- readFile "data/advent08.txt"
9 let digits = successfulParse text
10 let layers = chunksOf (imageWidth * imageHeight) digits
11 print $ part1 layers
12 putStrLn $ part2 layers
13
14 imageWidth = 25
15 imageHeight = 6
16
17 part1 layers = (count 1 target) * (count 2 target)
18 where target = minimumBy (comparing (count 0)) layers
19
20 part2 layers = unlines rows
21 where pixelLayers = transpose layers
22 pixels = map firstVisible pixelLayers
23 image = concatMap showPixel pixels
24 rows = chunksOf imageWidth image
25
26 firstVisible = head . dropWhile (== 2)
27
28 showPixel 0 = " "
29 showPixel 1 = "\x2588"
30
31 count n = length . filter (== n)
32
33 successfulParse :: String -> [Int]
34 successfulParse input = map digitToInt input