X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=app%2Fadvent14.hs;fp=app%2Fadvent14.hs;h=0000000000000000000000000000000000000000;hb=fd498a2713d69a5d55179ff07e58ce296d6fba94;hp=c6c092eead4f8a22eae0d4e05d7a21b7404fd9a6;hpb=3a26b187d5dc23b05fb73daabe52a92976a7a3c7;p=advent-of-code-16.git diff --git a/app/advent14.hs b/app/advent14.hs deleted file mode 100644 index c6c092e..0000000 --- a/app/advent14.hs +++ /dev/null @@ -1,43 +0,0 @@ -module Main(main) where - -import Data.List (nub, tails, null) -import Data.Hash.MD5 (md5s, Str(..)) - -salt = "yjdafjpo" --- salt = "abc" - -main :: IO () -main = do - part1 - part2 - -part1 :: IO () -part1 = print $ head $ drop 63 $ filter (\i -> possibleKey sq i && confirmKey sq i) [0..] - where sq = md5sequence - -part2 :: IO () -part2 = print $ head $ drop 63 $ filter (\i -> possibleKey sq i && confirmKey sq i) [0..] - where sq = md5sequenceS - -md5sequence :: [String] -md5sequence = [makeMd5 i | i <- [0..]] - where makeMd5 i = md5s (Str (salt ++ show i)) - -md5sequenceS :: [String] -md5sequenceS = [makeMd5 i | i <- [0..]] - where makeMd5 i = stretch $ md5s (Str (salt ++ show i)) - stretch h0 = foldr (\_ h -> md5s (Str h)) h0 [1..2016] - -possibleKey :: [String] -> Int-> Bool -possibleKey s = not . null . repeats 3 . ((!!) s) - -confirmKey :: [String] -> Int -> Bool -confirmKey s i = any (confirmation) $ take 1000 $ drop (i+1) s - where c = head $ repeats 3 $ s!!i - confirmation m = c `elem` (repeats 5 m) - -repeats :: Int -> String -> [String] -repeats n = filter (null . tail) . map (nub) . substrings n - -substrings :: Int -> [a] -> [[a]] -substrings l = filter (\s -> (length s) == l) . map (take l) . tails