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.

11.py 813B

12345678910111213141516171819202122232425262728293031
  1. import collections
  2. reductions = [
  3. # Simplifications
  4. [ 'n', 'se', 'ne'],
  5. [ 'n', 'sw', 'nw'],
  6. [ 's', 'ne', 'se'],
  7. [ 's', 'nw', 'sw'],
  8. ['sw', 'se', 's'],
  9. ['nw', 'ne', 'n'],
  10. # Opposites
  11. [ 'n', 's', '--'],
  12. ['sw', 'ne', '--'],
  13. ['nw', 'se', '--'],
  14. ]
  15. def distance(steps):
  16. counts = collections.defaultdict(lambda: 0, collections.Counter(steps))
  17. for a, b, result in reductions * 2:
  18. count = min(counts[a], counts[b])
  19. counts[a] -= count
  20. counts[b] -= count
  21. counts[result] += count
  22. return sum(counts.values()) - counts['--']
  23. with open('data/11.txt', 'r') as file:
  24. steps = file.readline().strip().split(',')
  25. print(f'Part one: {distance(steps)}')
  26. print(f'Part two: {max(distance(steps[:i]) for i in range(len(steps)))}')