Advent Of Code 2015: Day 4

Advent Of Code 201514950

The Challenge

--- Day 4: The Ideal Stocking Stuffer ---

Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically forward-thinking little girls and boys.

To do this, he needs to find MD5 hashes which, in hexadecimal, start with at least five zeroes. The input to the MD5 hash is some secret key (your puzzle input, given below) followed by a number in decimal. To mine AdventCoins, you must find Santa the lowest positive number (no leading zeroes: 1, 2, 3, ...) that produces such a hash.

For example:

If your secret key is abcdef, the answer is 609043, because the MD5 hash of abcdef609043 starts with five zeroes (000001dbbfa...), and it is the lowest such number to do so.
If your secret key is pqrstuv, the lowest number it combines with to make an MD5 hash starting with five zeroes is 1048970; that is, the MD5 hash of pqrstuv1048970 looks like 000006136ef....
Your puzzle answer was 346386.

--- Part Two ---

Now find one that starts with six zeroes.

Solution

Because speed was not an issue in this case, I simply used python again to solve the problem. The script is a little slow taking ~10 seconds to compute the answer in my case. A C/C++ solution would be orders of magnitude faster.

# Day 4 - Parts 1 and 2
import hashlib

key = 'iwrupvqb'
i = 0 #counter
five_zero_index = six_zero_index =  0

# Loop until criteria met
while True:
  i += 1
  hash = hashlib.md5(key + str(i)).hexdigest()
  
  if not five_zero_index and hash.startswith('0' * 5):
    five_zero_index = i
  if not six_zero_index and hash.startswith('0' * 6):
    six_zero_index = i
    
  # exit
  if (five_zero_index and six_zero_index):
    break
  
# Print answers
print "Five zero index is:", five_zero_index
print "Six zero index is:", six_zero_index

Leave a comment

(required)(will not be published)(required)

Comments

There are no comments yet. Be the first to add one!