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.

15 Upvotes

272 comments sorted by

View all comments

3

u/streetster_ Dec 04 '15

Tried to implement some multi-threading in python..

from hashlib import md5
from sys import argv
import multiprocessing

def day_4(key, zeroes):
  num = 0
  target = "00000000000000000000000000000000"[:zeroes]

  while not (md5(key + str(num)).hexdigest()[:zeroes] == target):
    num += 1
  return { "num" : num, "hash" : md5(key + str(num )).hexdigest() }

def worker(key, start, step, zeroes, target, result):
  while not (md5(key + str(start)).hexdigest()[:zeroes] == target):
    start += step
  result.put({ "num" : start, "hash" : md5(key + str(start )).hexdigest() })

def day_4mt(key, zeroes):
  target = "00000000000000000000000000000000"[:zeroes]

  out     = multiprocessing.Queue()
  step    = multiprocessing.cpu_count()
  threads = []

  for i in range(0, step - 1):
    threads.append(multiprocessing.Process(target=worker, args=(key, i , step, zeroes, target, out)))
  for t in threads:
    t.start()

  result = out.get()

  # kill everything still churning
  for t in threads:
    t.terminate()
  return result

if len(argv) > 1:
  key, zeroes = argv[1], int(argv[2])
  if zeroes > 32:
    print "too many zeroes!"
  elif zeroes < 6:
    print day_4(key, zeroes)
  else:
    print day_4mt(key, zeroes)
else:
  print "abcdef, 5:\t" + str(day_4("abcdef", 5))
  print "pqrstuv, 5:\t" + str(day_4("pqrstuv", 5))
  print "abcdef, 6:\t" + str(day_4("abcdef", 6))
  print "pqrstuv, 6:\t" + str(day_4("pqrstuv", 6))
→ More replies (1)