-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.hs
53 lines (47 loc) · 1.58 KB
/
Day14.hs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Day14
( part1
, part2
, stretch
) where
import Crypto.Hash.MD5 (hash)
import qualified Data.ByteString as BS (unpack)
import Data.ByteString.Base16 (encode)
import Data.List as L (init, isInfixOf)
import Data.Maybe (fromJust, isJust)
import Data.Text (Text, append)
import qualified Data.Text as T (init)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Data.Word (Word8)
import MD5 (dedup, md5Concat)
import Numeric (showHex)
import TextShow (showt)
saltHash :: Text -> Int -> [Word8]
saltHash salt = flip md5Concat salt . showt
findKeys :: Int -> [[Word8]] -> [Int]
findKeys index (h:ashes)
| isKey = index : findKeys (index + 1) ashes
| otherwise = findKeys (index + 1) ashes
where
isKey =
isJust (findThree h)
&& any
(replicate 5 (fromJust . findThree $ h) `isInfixOf`)
(take 1000 ashes)
findThree [a, b] = Nothing
findThree (a:xs@(b:c:_))
| a == b && a == c = Just a
| otherwise = findThree xs
stretch :: Text -> Int -> [Word8]
stretch a =
dedup
. BS.unpack
. (!! 2016)
. iterate (hash . encode)
. hash
. encodeUtf8
. append a
. showt
part1 :: Bool -> Text -> String
part1 _ = show . (!! 63) . findKeys 0 . flip map [0 ..] . saltHash . T.init
part2 :: Bool -> Text -> String
part2 _ = show . (!! 63) . findKeys 0 . flip map [0 ..] . stretch . T.init