Advent of Code 2016 solutions https://adventofcode.com/2016/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/python
  2. from enum import IntEnum
  3. import math
  4. import operator
  5. def steps(start, delta, count):
  6. sign = int(math.copysign(1, delta))
  7. return [start] * count if delta == 0 else range(start + sign, start + delta + sign, sign)
  8. with open('01.txt', 'r') as file:
  9. input = file.read()
  10. movement_lookup = {'L': -1, 'R': +1}
  11. direction_lookup = {0: (0, -1), 1: (1, 0), 2: (0, 1), 3: (-1, 0)}
  12. moves = [(movement_lookup[move[0]], int(move[1:])) for move in input.split(', ')]
  13. heading = 0
  14. history = [(0, 0)]
  15. for move in moves:
  16. heading = (heading + move[0]) % 4
  17. delta = map(operator.mul, direction_lookup[heading], (move[1], move[1]))
  18. delta_mag = max(abs(delta[0]), abs(delta[1]))
  19. history += zip(steps(history[-1][0], delta[0], delta_mag),
  20. steps(history[-1][1], delta[1], delta_mag))
  21. duplicates = (x for n, x in enumerate(history) if history[:n].count(x) > 0)
  22. overlap = next(duplicates)
  23. print("Part 1: %s ==> %s" % (history[-1], abs(history[-1][0]) + abs(history[-1][1])))
  24. print("Part 2: %s ==> %s" % (overlap, abs(overlap[0]) + abs(overlap[1])))