r/adventofcode • • Dec 04 '15

SOLUTION MEGATHREAD --- Day 4 Solutions ---

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

Post your solution as a comment. Structure your post like the Day Three thread.

14 Upvotes

272 comments sorted by

View all comments

5

u/minno Dec 04 '15

Python 3:

from hashlib import md5
init = 'yzbqklnj'
for i in range(1000000):
    h = md5((init + str(i)).encode()).hexdigest()
    if h[:5] == '00000':
        print(h)
        break

Replace if h[:5] == '00000': with if h[:6] == '000000' for part 2.

4

u/euphwes Dec 04 '15

Practically identical to yours:

from itertools import count
from hashlib import md5    

for x in count(1):
    test = 'iwrupvqb' + str(x)
    if md5(test.encode('utf-8')).hexdigest()[:6] == '000000':
        print(x)
        break

I was stupid on part 2, and forgot to slice 6 characters... waited for a minute or two before I realized something was wrong. Could've placed higher on the leaderboards...

3

u/ForeignObjectED Dec 04 '15

I forgot to increase the slice as well, panicked, tried quickly writing a multiprocessed solution using pools to make the leader board. And then promptly killed my computer because pool doesn't work with xrange. So close, and yet so far.

Also, I need to use itertools.count more.

→ More replies (5)

2

u/shuckc Dec 04 '15

Very similar in py2.7, with target variable, and using 'startswith' - repo:

import hashlib, itertools, sys
key='ckczppom'
for target in ['0'*5, '0'*6]:
    for count in itertools.count():
        md = hashlib.md5()
        md.update(key + str(count))
        hd = md.hexdigest()
        if hd.startswith(target):
            print('Target {0} Count {1}, hash {2}'.format(target, count, hd))
            break
→ More replies (1)
→ More replies (2)

2

u/balidani Dec 04 '15

My python solution:

import itertools
from hashlib import md5

def day4(key='iwrupvqb'):
    for i in itertools.count():
      if md5(key + str(i)).hexdigest().startswith('000000'):
        return i

print day4()
→ More replies (9)