Advent of Code 2016 solutions https://adventofcode.com/2016/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

14.py 910B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/python3
  2. import functools
  3. import hashlib
  4. import itertools
  5. import re
  6. salt = 'cuanljph'
  7. @functools.lru_cache(maxsize=1024)
  8. def md5(index):
  9. return hashlib.md5((salt + str(index)).encode('UTF-8')).hexdigest()
  10. @functools.lru_cache(maxsize=1024)
  11. def stretched(index):
  12. key = salt + str(index)
  13. for _ in range(2017): key = hashlib.md5(key.encode('UTF-8')).hexdigest()
  14. return key
  15. @functools.lru_cache(maxsize=1024)
  16. def trip(index, fn):
  17. return tripmatcher.search(fn(index))
  18. tripmatcher = re.compile(r'(.)\1{2}')
  19. matches = lambda fn: ((i, fn(i)) for i in itertools.count()
  20. if trip(i, fn) and any(trip(i, fn).group(1) * 5 in fn(n)
  21. for n in range(i + 1, i + 1000)))
  22. print("Part one: %s" % next(itertools.islice(matches(md5), 63, 64))[0])
  23. print("Part two: %s" % next(itertools.islice(matches(stretched), 63, 64))[0])