Solutions to Advent of Code 2017 https://adventofcode.com/2017/
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.

10.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import functools
  2. import itertools
  3. import operator
  4. def reverse_slice(input, start, length):
  5. output = list(input)
  6. for i, pos in enumerate(range(start, start + length)):
  7. output[pos % len(output)] = input[(start + length - i - 1) % len(output)]
  8. return output
  9. def do_rounds(lengths, count=1):
  10. position, skip, data = 0, 0, list(range(256))
  11. for _ in range(count):
  12. for length in lengths:
  13. data = reverse_slice(data, position, length)
  14. position += length + skip
  15. skip += 1
  16. return data
  17. def group(n, iterable):
  18. # https://docs.python.org/3.1/library/itertools.html#recipes
  19. args = [iter(iterable)] * n
  20. return itertools.zip_longest(*args)
  21. with open('data/10.txt', 'r') as file:
  22. lengths = list(map(int, file.readline().strip().split(',')))
  23. data = do_rounds(lengths)
  24. print(f'Part one: {data[0]*data[1]}')
  25. with open('data/10.txt', 'r') as file:
  26. lengths = list(map(ord, file.readline().strip())) + [17, 31, 73, 47, 23]
  27. sparse_hash = do_rounds(lengths, 64)
  28. dense_hash = [functools.reduce(operator.xor, g) for g in group(16, sparse_hash)]
  29. # Yo dawg, I heard you like f-strings, so we put an f-string in your f-string.
  30. print(f'Part two: {"".join(f"{c:02x}" for c in dense_hash)}')