-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay4.hs
More file actions
36 lines (30 loc) · 1.04 KB
/
Day4.hs
File metadata and controls
36 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module Day4
( part1
, part2
) where
import Crypto.Hash.MD5 (hash)
import Data.Bits (shiftR, (.&.))
import Data.ByteString (ByteString, append, concat)
import qualified Data.ByteString as BS (init, take, unpack)
import qualified Data.ByteString.Base16 as BSB (encode)
import Data.ByteString.UTF8 (fromString)
import Debug.Trace
md5Concat :: Int -> ByteString -> Int -> Bool
md5Concat zeroSize salt value = all (== 0) . take zeroSize $ md5Hash
where
md5Hash =
concatMap (\i -> [shiftR i 4, i .&. 15])
. BS.unpack
. BS.take halfSize
. hash
. append salt
. fromString
. show
$ value
halfSize = div zeroSize 2 + mod zeroSize 2
findFirst :: Int -> ByteString -> Int
findFirst zeroSize input = head . filter (md5Concat zeroSize input) $ [0 ..]
part1 :: Bool -> ByteString -> String
part1 _ = show . findFirst 5 . BS.init
part2 :: Bool -> ByteString -> String
part2 _ = show . findFirst 6 . BS.init