X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=adventofcode16%2Fapp%2Fadvent05.hs;fp=adventofcode16%2Fapp%2Fadvent05.hs;h=82a035eaf368b91d183c4fd85773588f749241cc;hb=fd498a2713d69a5d55179ff07e58ce296d6fba94;hp=0000000000000000000000000000000000000000;hpb=3a26b187d5dc23b05fb73daabe52a92976a7a3c7;p=advent-of-code-16.git diff --git a/adventofcode16/app/advent05.hs b/adventofcode16/app/advent05.hs new file mode 100644 index 0000000..82a035e --- /dev/null +++ b/adventofcode16/app/advent05.hs @@ -0,0 +1,44 @@ +module Main(main) where + +import Data.Hash.MD5 (md5s, Str(..)) +import Data.List (isPrefixOf) +import qualified Data.Map.Lazy as Map + +type Password = Map.Map Integer Char + +input = "cxdnnyjw" + +main :: IO () +main = do + part1 + part2 + + +part1 :: IO () +part1 = do + putStrLn $ take 8 [h!!5 | h <- filter (interesting) $ md5sequence input 0] + +part2 :: IO () +part2 = do + putStrLn $ Map.foldr (:) [] password + where interestingHashes = + [(read [h!!5], h!!6) | + h <- filter (interesting) (md5sequence input 0), + h!!5 `elem` "01234567"] + password = findPassword Map.empty interestingHashes + + +md5sequence :: String -> Integer -> [String] +md5sequence key i = (md5s (Str (key ++ show i))) : (md5sequence key (i+1)) + +interesting :: String -> Bool +interesting hash = "00000" `isPrefixOf` hash + +dontReplace :: (Integer, Char) -> Password -> Password +dontReplace (k, v) = Map.insertWith (\_ v -> v) k v + +findPassword :: Password -> [(Integer, Char)] -> Password +findPassword p (c:cs) + | Map.size p == 8 = p + | otherwise = findPassword p' cs + where p' = dontReplace c p