X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-19.git;a=blobdiff_plain;f=advent08%2Fsrc%2Fadvent08.hs;fp=advent08%2Fsrc%2Fadvent08.hs;h=fe00af5cb8e9518498b08d14552de30a5be25f8d;hp=0000000000000000000000000000000000000000;hb=230f8fde364d05f31df481c711f6e7eb086259a5;hpb=8a26938bdc252b56b78d267756c5a95aab97bf5f diff --git a/advent08/src/advent08.hs b/advent08/src/advent08.hs new file mode 100644 index 0000000..fe00af5 --- /dev/null +++ b/advent08/src/advent08.hs @@ -0,0 +1,49 @@ +import Data.List +import Data.List.Split +import Data.Char +import Data.Ord +import Data.Function + +main :: IO () +main = do + text <- readFile "data/advent08.txt" + let digits = successfulParse text + let layers = chunksOf (imageWidth * imageHeight) digits + print $ part1 layers + putStrLn $ part2 layers + + +imageWidth = 25 +imageHeight = 6 + +part1 layers = (count 1 target) * (count 2 target) + where target = minimumBy (comparing (count 0)) layers + +part2 layers = unlines rows + where pixelLayers = transpose layers + pixels = map firstVisible pixelLayers + dPixels = map showPixel pixels + pixelRows = chunksOf imageWidth dPixels + rows = map concat pixelRows + + +firstVisible = head . dropWhile (== 2) + +showPixel 0 = " " +showPixel 1 = "\x2588" + + +count n = length . filter (== n) + +-- Count the number of times a predicate is true +-- (Taken from GHC API utility functions) + +-- count :: (a -> Bool) -> [a] -> Int +-- count p = go 0 +-- where go !n [] = n +-- go !n (x:xs) | p x = go (n+1) xs +-- | otherwise = go n xs + + +successfulParse :: String -> [Int] +successfulParse input = map digitToInt input \ No newline at end of file