Day 4

Part 1

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 input is ckczppom.


In [3]:
input=: 'ckczppom'
require '~addons/ide/qt/qt.ijs'
getmd5=: [: gethash_jqtide_ 'md5'&;

makemd5hash=: [: getmd5 (, ":) 
testfirst0s=: (#&'0'@[ -.@-: {.)

NB. >: ^: (5 testfirst0s 'abcdef' makemd5hash ])^:_ ] 1
NB. >: ^: (5 testfirst0s 'pqrstuv' makemd5hash ])^:_ ] 1

findhash=: 4 : '>: ^: (x testfirst0s y makemd5hash ])^:_ ] 1'
echo 'Day 4 Part 1: ',": 5 findhash input


Day 4 Part 1: 117946

Part 2

Now find one that starts with six zeroes.


In [4]:
echo 'Day 4 Part 2: ',": 6 findhash input


Day 4 Part 2: 3938038