<commit_before>import Data.List (isPrefixOf, find, sortOn)
import Data.ByteString.Char8 (pack)
import Crypto.Hash
import Data.HashMap.Strict (HashMap, insertWith, empty, toList)
import Data.Char (isDigit, digitToInt)

-- I guess this one can be significantly refactored and optimized

md5 :: String -> Digest MD5
md5 = hash . pack

handlePassword :: (HashMap Int Char, String) -> String -> (HashMap Int Char, String)
handlePassword (positioned, code) password =
    let indexChar:rest = drop 5 $ password
        positioned2 = 
            if (not $ null rest ) && isDigit indexChar && indexChar < '8' 
            then insertWith (\new old -> old) (digitToInt indexChar) (head rest) positioned 
            else positioned
        code2 = if length code < 8 then indexChar : code else code
    in (positioned2, code2)

solve :: String  -> (String, String)
solve prefix =
    let combine n = prefix ++ show n
        passwords = filter (isPrefixOf "00000") $ map (show . md5 . combine) $ iterate (+1) 0
        combined = drop 1 $ scanl handlePassword (empty, []) passwords<commit_msg>Slightly simplify day5 solution<commit_after>import Data.List (isPrefixOf, find, sortOn)
import Data.ByteString.Char8 (pack)
import Crypto.Hash (hashWith)
import Crypto.Hash.Algorithms (MD5)
import Data.HashMap.Strict (HashMap, insertWith, empty, toList)
import Data.Char (isDigit, digitToInt)

-- I guess this one can be significantly refactored and optimized

handlePassword :: (HashMap Int Char, String) -> String -> (HashMap Int Char, String)
handlePassword (positioned, code) password =
    let indexChar:rest = drop 5 password
        positioned2 = 
            if not (null rest) && isDigit indexChar && indexChar < '8' 
            then insertWith (\_ old -> old) (digitToInt indexChar) (head rest) positioned 
            else positioned
        code2 = if length code < 8 then indexChar : code else code
    in (positioned2, code2)

solve :: String  -> (String, String)
solve prefix =
    let md5 = show . hashWith (undefined :: MD5) . pack . (prefix ++) . show
        passwords = filter (isPrefixOf "00000") $ map md5 $ iterate (+1) 0
        combined = drop 1 $ scanl handlePassword (empty, []) passwords
{'fraction_non_alphanumeric': 0.1008174386920981, 'fraction_numerical': 0.018619436875567667, 'mean_word_length': 3.261121856866538, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4015171', 'n_tokens_mistral': 658, 'n_tokens_neox': 637, 'n_words': 239}