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

Show parent comment

1

u/[deleted] Dec 04 '15 edited Dec 04 '15

Python implimention with concurrent futures process pool executors, i quite simply just split a range of iterators to each threads and iterate until i found the result, completes challenge 2 in about 4 seconds.

from concurrent import futures
import hashlib

def stuffit(input_begin, hash_begin, range):
    for i in range:
        digest = hashlib.md5((input_begin + str(i)).encode())
        result = digest.hexdigest()
        if result[:len(hash_begin)] == hash_begin:
            print('input', input_begin + str(i))
            print('result', result)

def find_stuffer(input_begin, hash_begin, nmax):
    threads = 6
    with futures.ProcessPoolExecutor(max_workers=threads) as executor:
        stuffed_futures = []
        for thread in range(threads):

            # using steps to devise the ranges between threads
            r = range(1 + thread, 10**nmax + thread, threads)
            f = executor.submit(stuffit, input_begin, hash_begin, r)

            stuffed_futures.append(f)
        futures.wait(stuffed_futures, return_when=futures.ALL_COMPLETED)

# search range will iterate up to 10 ** nmax
# 7 is enough to find the fisrt '000000' md5
find_stuffer('yzbqklnj', '000000', 7)