summaryrefslogtreecommitdiff
path: root/2019/8.hs
blob: e9f269b6e6fe1baa773fc1ca5845323d80bebc34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Main where

import Data.List
import Data.Ord

import Input


blockBy :: Int -> [a] -> [[a]]
blockBy _ [] = []
blockBy n l = uncurry (:) (fmap (blockBy n) (splitAt n l))

main :: IO ()
main = do
    layers <- blockBy (25 * 6) . head <$> getInput 8

    let count x = length . filter (== x)
        layer = minimumBy (comparing (count '0')) layers
    print (count '1' layer * count '2' layer)

    let combine = zipWith (\a b -> if a == '2' then b else a)
        image = foldl1 combine layers
        image' = map (\c -> if c == '1' then '#' else '.') image
    mapM_ putStrLn (blockBy 25 image')