Advent of Code 2016 solutions https://adventofcode.com/2016/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

18.py 1.2KB

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/python3
  2. """Solution for day 18 of Advent of Code 2016.
  3. Each line of the maze is represented as a list of booleans, with True for safe spaces and False for traps.
  4. The four separate trap rules can be simplified to just "left is different to right", which is handled in the line
  5. method (with some annoying fudging to handle the first and last elements whose parents are 'walls').
  6. The solution is obtained by repeatedly generating lines and summing them (Python booleans are a subclass of ints,
  7. with True being equal to 1 and False to 0, so a simple call to sum counts the number of safe squares). Only the most
  8. recent line is kept, everything else is discarded once counted.
  9. """
  10. seed = [x == '.' for x in
  11. '^^.^..^.....^..^..^^...^^.^....^^^.^.^^....^.^^^...^^^^.^^^^.^..^^^^.^^.^.^.^.^.^^...^^..^^^..^.^^^^']
  12. def line(previous):
  13. return [(i == 0 or previous[i - 1]) == (i == len(previous) - 1 or previous[i + 1]) for i in range(len(previous))]
  14. def calculate(num):
  15. last_line = seed
  16. safe_spaces = sum(last_line)
  17. lines = 1
  18. while lines < num:
  19. last_line = line(last_line)
  20. safe_spaces += sum(last_line)
  21. lines += 1
  22. return safe_spaces
  23. print('Part one: %s' % calculate(40))
  24. print('Part two: %s' % calculate(400000))