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.

13.py 1.0KB

123456789101112131415161718192021222324252627
  1. #!/usr/bin/python3
  2. """Solution for day 13 of Advent of Code 2016.
  3. Performs a breadth-first search of the maze, for up to 100 steps. Distances of each square are kept in a dictionary,
  4. which is also used to prevent back-tracking.
  5. """
  6. import itertools
  7. input = 1364
  8. high = lambda x: sum([1 for d in bin(x) if d == '1'])
  9. wall = lambda x, y: high(x*x + 3*x + 2*x*y + y + y*y + input) % 2 == 1
  10. all_moves = lambda x, y: [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
  11. moves = lambda x, y: (m for m in all_moves(x, y) if m[0] >= 0 and m[1] >= 0 and not wall(*m))
  12. queue = {(1, 1)}
  13. distance = {}
  14. for step in range(100):
  15. # Merge in all the new distances from the queue, if they're not present
  16. distance = dict(list(distance.items()) + list(dict.fromkeys(queue, step).items()))
  17. # Calculate all moves from all places in the queue, and skip any places we've already visited
  18. queue = set(itertools.chain.from_iterable(moves(*c) for c in queue)) - set(distance.keys())
  19. print("Part 1: %s" % distance[(31, 39)])
  20. print("Part 2: %s" % sum(1 for s in distance.values() if s <= 50))