-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8.hs
51 lines (44 loc) · 1.19 KB
/
Day8.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module Day8 where
import Utils (wordsEvery, count)
import Data.List (minimumBy)
data Image =
Image
{ _width :: Int
, _height :: Int
, _layers :: [String]
}
parseImage :: (Int, Int) -> String -> Image
parseImage (w, h) s =
Image { _width = w
, _height = h
, _layers = wordsEvery (w * h) s
}
part1 :: Image -> Int
part1 image =
let zeroesCounts = map (count '0') (_layers image)
layer =
(_layers image !!) $
fst $
minimumBy (\a b -> compare (snd a) (snd b)) $
zip [0..] zeroesCounts
in count '1' layer * count '2' layer
getPixel :: Int -> Image -> Char
getPixel index image =
let pixels = map (!! index) (_layers image)
top = dropWhile (== '2') pixels
in if null top
then ' '
else case head top of
'0' -> '#'
'1' -> '.'
part2 :: Image -> String
part2 image =
let pixels = map (`getPixel` image) [0..(_width image * _height image) - 1]
in unlines (wordsEvery (_width image) pixels)
main :: IO ()
main = do
contents <- readFile "input/day8.txt"
let image = parseImage (25, 6) contents
putStrLn ("Part 1: " ++ show (part1 image))
putStrLn "Part 2:"
putStrLn (part2 image)