Tuesday, December 25, 2018

Advent of Code 2015 - Day 4: The Ideal Stocking Stuffer

Part 1

Description

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....

Input


Solution

The input for this puzzle is relatively short. But, the difficulty to obtain the answer is incredibly painful. Firstly, because I don't really know what on earth MD5 hash is (even after reading about it) and how it is obtained. Secondly, MS Excel doesn't have a ready-to-use function to obtain MD5 hash value. So, my Excel only looked like this:


Those two buttons are for running the Macros, something I added later.
After reading and searching for few hours, I found this file in this website. But, after carefully examined it, I decided not to use it because it only supported finding the MD5 hash value for a string in one cell. Modifying it to serve my purpose also seemed impossible because I don't understand a dime about it. Therefore I tried to find a VBA code for generating that MD5 hash and found it here. The code is quite long, so that I will not post it here.
Now, after found the code, all left is to add a Macro to run the code until the hash reached the first value starting with five zeroes. Here is my Macro:

Sub Hitung()
    Dim input4, output4 As String
    Dim i As Double
    
    i = 0
Start:
    input4 = "iwrupvqb" & i
    output = MD5Hash(input4)
    Debug.Print output
    i = i + 1
    If Mid(output, 1, 5) = "00000" Then
        GoTo Finish
    Else
        GoTo Start
    End If
Finish:
    ActiveSheet.Range("b4").Select
    ActiveCell.Value = i - 1
End Sub

Part 2

Description

Now find one that starts with six zeroes.

Solution

And, the macro for the second part:

Sub Hitung2()
    Dim input4, output4 As String
    Dim i As Double
    
    i = 0
Start:
    input4 = "iwrupvqb" & i
    output = MD5Hash(input4)
    Debug.Print output
    i = i + 1
    If Mid(output, 1, 6) = "000000" Then
        GoTo Finish
    Else
        GoTo Start
    End If
Finish:
    ActiveSheet.Range("b5").Select
    ActiveCell.Value = i - 1
End Sub

Share:

0 komentar:


Recent Posts

Categories

Blog Archive