-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.hs
39 lines (33 loc) · 1.25 KB
/
Day5.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
module Day5
( part1
, part2
) where
import Data.Bits (shiftR, (.&.))
import Data.List (foldl', scanl', sort)
import Data.Maybe (fromJust)
import Data.Text (Text)
import qualified Data.Text as T (init)
import Data.Text.Encoding (decodeUtf8)
import Data.Word (Word8)
import GHC.Conc (numCapabilities)
import MD5 (md5ConcatTake)
import Numeric (showHex)
import TextShow (showt)
md5Prefix :: [Int] -> Text -> [[Word8]]
md5Prefix a b =
filter (all (== 0) . take 5) . map (flip (md5ConcatTake 4) b . showt) $ a
decode :: [[Word8]] -> String
decode =
foldr (showHex . snd) ""
. sort
. head
. dropWhile ((< 8) . length)
. scanl' (flip (compile . (\[a, b] -> (a, b)) . take 2 . drop 5)) []
compile :: (Word8, Word8) -> [(Word8, Word8)] -> [(Word8, Word8)]
compile pair@(index, _) found
| index > 7 || index `elem` map fst found = found
| otherwise = pair : found
part1 :: Bool -> Text -> String
part1 _ = foldr showHex "" . take 8 . map (!! 5) . md5Prefix [0 ..] . T.init
part2 :: Bool -> Text -> String
part2 _ = decode . md5Prefix [0 ..] . T.init