Created
December 8, 2019 11:38
-
-
Save saevarb/42a8ef2b30091e3b9a93692d724d896d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Day8 where | |
import qualified Data.Text as T | |
import qualified Data.Map.Strict as M | |
import Data.Monoid | |
import Data.List | |
import Data.Ord (comparing) | |
import Data.Char (digitToInt) | |
import Data.Maybe (fromMaybe, catMaybes) | |
import Util | |
count :: (Ord k, Num a) => [k] -> M.Map k a | |
count xs = M.fromListWith (+) $ zip xs (repeat 1) | |
run :: IO () | |
run = do | |
inp <- map (map digitToInt . T.unpack) <$> readInput "day8" (T.chunksOf (25 * 6)) | |
print $ part1 inp | |
mapM_ print $ part2 inp | |
where | |
part1 :: [[Int]] -> Int | |
part1 i = | |
let m = minimumBy (comparing (fetch 0)) $ map count i | |
(ones, twos) = (fetch 1 m, fetch 2 m) | |
in ones * twos | |
fetch :: Ord k => k -> M.Map k Int -> Int | |
fetch k m = fromMaybe 0 $ M.lookup k m | |
part2 i = | |
map catMaybes | |
. chunk 25 | |
. map (getFirst . mconcat) | |
. transpose | |
$ map (map frobnicate) i | |
frobnicate 2 = First Nothing | |
frobnicate x = First $ Just x | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment