advent-of-code/2015/haskell/solutions/Day04.hs

30 lines
763 B
Haskell
Raw Normal View History

2024-08-06 22:29:35 +02:00
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings #-}
2024-08-04 15:06:49 +02:00
module Day04 (solveA, solveB) where
import Crypto.Hash.MD5 (hash)
import Data.Bifunctor (second)
import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
import Data.ByteString.Char8 qualified as Char8
import Text.Printf (printf)
2024-08-06 22:29:35 +02:00
hashHex :: ByteString -> String
hashHex = concatMap (printf "%02x") . BS.unpack . hash
2024-08-04 15:06:49 +02:00
2024-08-06 22:29:35 +02:00
test :: ByteString
2024-08-04 15:06:49 +02:00
test = "ckczppom"
2024-08-06 22:29:35 +02:00
solve :: Int -> ByteString -> Integer
2024-08-04 15:06:49 +02:00
solve zeros key = fst . head . filter (isMatch . snd) . map toHash $ [1 ..]
where
2024-08-06 22:29:35 +02:00
toHash n = (n, hashHex $ key <> Char8.pack (show n))
2024-08-04 15:06:49 +02:00
isMatch = all (== '0') . take zeros
2024-08-06 22:29:35 +02:00
solveA :: ByteString -> Integer
2024-08-04 15:06:49 +02:00
solveA = solve 5
2024-08-06 22:29:35 +02:00
solveB :: ByteString -> Integer
2024-08-04 15:06:49 +02:00
solveB = solve 6