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.

16.py 890B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/python3
  2. """Solution for day 16 of Advent of Code 2016.
  3. This is a straightforward, brute-searching solution. It expands the input as defined in the puzzle, and then
  4. computes checksums until it finds an odd-length one.
  5. """
  6. import itertools
  7. def expand(state, length):
  8. while len(state) < length:
  9. state = list(itertools.chain(state, [False], (not x for x in reversed(state))))
  10. return state[:length]
  11. def checksum_round(state):
  12. return [state[i] == state[i + 1] for i in range(0, len(state), 2)]
  13. def checksum(state):
  14. state = checksum_round(state)
  15. while len(state) % 2 == 0:
  16. state = checksum_round(state)
  17. return state
  18. def run(state, size):
  19. return ''.join(str(int(x)) for x in checksum(expand(state, size)))
  20. iv = [bool(int(x)) for x in '01110110101001000']
  21. print('Step 1: %s' % run(iv, 272))
  22. print('Step 2: %s' % run(iv, 35651584))