2024-12-04 17:15:59 +11:00
|
|
|
{-# OPTIONS_GHC -Wno-x-partial #-}
|
|
|
|
|
2024-12-03 00:24:29 +11:00
|
|
|
import Aoc
|
|
|
|
import Crypto.Hash.MD5 ( hash )
|
|
|
|
import Data.ByteString.Char8 ( pack )
|
|
|
|
import Data.ByteString.Builder ( byteStringHex, toLazyByteString )
|
|
|
|
import Data.ByteString.Lazy.Char8 ( pack, isPrefixOf )
|
|
|
|
|
|
|
|
parseFile :: [String] -> [Char]
|
|
|
|
parseFile = single
|
|
|
|
|
|
|
|
mine :: Int -> [Char] -> Int
|
|
|
|
mine n key = head $ filter matches [1..]
|
|
|
|
where
|
|
|
|
zeros = Data.ByteString.Lazy.Char8.pack $ replicate n '0'
|
|
|
|
matches = isPrefixOf zeros . toLazyByteString . byteStringHex . hash . Data.ByteString.Char8.pack . (key++) . show
|
|
|
|
|
|
|
|
part1 :: [Char] -> Int
|
|
|
|
part1 = mine 5
|
|
|
|
|
|
|
|
part2 :: [Char] -> Int
|
|
|
|
part2 = mine 6
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = aocMain parseFile part1 part2
|