{-# LANGUAGE OverloadedStrings #-}

module Five (five) where

import Numeric
import Data.List
import Data.Semigroup
import Control.Monad
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import Crypto.Hash.MD5

-- XXX this should be in a custom prelude zzz
prettyHex :: B.ByteString -> String
prettyHex = concat . (map (\s -> if length s == 1 then '0':s else s)) . (map (flip showHex "")) . B.unpack

prefixPred :: Integer -> (String -> Bool)
prefixPred n = isPrefixOf ("00000" ++ show n)

five = do
    let input = "uqwqemis"
        zeros = filter (isPrefixOf "00000") $ (prettyHex . hash . (input <>) . BC.pack . show) <$> [0 ..]
        result1 = (!! 5) <$> (take 8 zeros)

    putStrLn $ "part A: " ++ result1

    case (fmap (!! 6)) <$> sequence (flip find zeros <$> (prefixPred <$> [0 .. 7])) of
        Just result2 -> putStrLn $ "part B: " ++ result2
        Nothing -> putStrLn "uhhh find didn't work"
