From fef438e522205404e0e765414e412ccacc34bfaa Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 5 Dec 2016 12:28:57 +0000 Subject: [PATCH] Day 5 --- README.html | 3 ++ README.md | 6 +++ advent05.hs | 44 +++++++++++++++ day05.html | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 advent05.hs create mode 100644 day05.html diff --git a/README.html b/README.html index 68668e7..754e9be 100644 --- a/README.html +++ b/README.html @@ -17,6 +17,9 @@

I'm using the basic Haskell Platform installation (install with

$ sudo aptitude install haskell-platform

).

+

I'm also using the MissingH library (install with

+
$ cabal install MissingH
+

)

Compile the code with

ghc --make advent01.hs

then run it as

diff --git a/README.md b/README.md index d95ac12..5c7f379 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,12 @@ $ sudo aptitude install haskell-platform ``` ). +I'm also using the `MissingH` library (install with +``` +$ cabal install MissingH +``` +) + Compile the code with ``` ghc --make advent01.hs diff --git a/advent05.hs b/advent05.hs new file mode 100644 index 0000000..d86bd5a --- /dev/null +++ b/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 + print $ take 8 [h!!5 | h <- filter (interesting) $ md5sequence input 0] + +part2 :: IO () +part2 = do + print $ 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 (candidate:candidates) + | Map.size p == 8 = p + | otherwise = findPassword p' candidates + where p' = dontReplace candidate p diff --git a/day05.html b/day05.html new file mode 100644 index 0000000..f8af5e5 --- /dev/null +++ b/day05.html @@ -0,0 +1,152 @@ + + + + +Day 5 - Advent of Code 2016 + + + + + + +

Advent of Code

Neil Smith (AoC++) 10*

       y(2016)

+ + + +
+

--- Day 5: How About a Nice Game of Chess? ---

You are faced with a security door designed by Easter Bunny engineers that seem to have acquired most of their security knowledge by watching hacking movies.

+

The eight-character password for the door is generated one character at a time by finding the MD5 hash of some Door ID (your puzzle input) and an increasing integer index (starting with 0).

+

A hash indicates the next character in the password if its hexadecimal representation starts with five zeroes. If it does, the sixth character in the hash is the next character of the password.

+

For example, if the Door ID is abc:

+
    +
  • The first index which produces a hash that starts with five zeroes is 3231929, which we find by hashing abc3231929; the sixth character of the hash, and thus the first character of the password, is 1.
  • +
  • 5017308 produces the next interesting hash, which starts with 000008f82..., so the second character of the password is 8.
  • +
  • The third time a hash starts with five zeroes is for abc5278568, discovering the character f.
  • +
+

In this example, after continuing this search a total of eight times, the password is 18f47a30.

+

Given the actual Door ID, what is the password?

+
+

Your puzzle answer was f77a0e6e.

--- Part Two ---

As the door slides open, you are presented with a second door that uses a slightly more inspired security mechanism. Clearly unimpressed by the last version (in what movie is the password decrypted in order?!), the Easter Bunny engineers have worked out a better solution.

+

Instead of simply filling in the password from left to right, the hash now also indicates the position within the password to fill. You still look for hashes that begin with five zeroes; however, now, the sixth character represents the position (0-7), and the seventh character is the character to put in that position.

+

A hash result of 000001f means that f is the second character in the password. Use only the first result for each position, and ignore invalid positions.

+

For example, if the Door ID is abc:

+
    +
  • The first interesting hash is from abc3231929, which produces 0000015...; so, 5 goes in position 1: _5______.
  • +
  • In the previous method, 5017308 produced an interesting hash; however, it is ignored, because it specifies an invalid position (8).
  • +
  • The second interesting hash is at index 5357525, which produces 000004e...; so, e goes in position 4: _5__e___.
  • +
+

You almost choke on your popcorn as the final character falls into place, producing the password 05ace8e3.

+

Given the actual Door ID and this new method, what is the password? Be extra proud of your solution if it uses a cinematic "decrypting" animation.

+
+

Your puzzle answer was 999828ec.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your advent calendar and try another puzzle.

+

Your puzzle input was cxdnnyjw.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1