5.py 804 B

1234567891011121314151617181920212223242526272829
  1. from util import get_input
  2. from collections import namedtuple
  3. from more_itertools import flatten
  4. input = get_input("5.input")
  5. Point = namedtuple("Point", ['x', 'y'])
  6. input = [[Point(int(pair.split(",")[0]), int(pair.split(",")[1])) for pair in line.split(" -> ")] for line in input]
  7. maxx = max([p.x for p in flatten(input)])
  8. maxy = max([p.y for p in flatten(input)])
  9. board = [[0 for _ in range(maxy + 1)] for _ in range(maxx + 1)]
  10. def sign(n):
  11. return 0 if n == 0 else (1 if n > 0 else -1)
  12. for line in input:
  13. dx = sign(line[1].x - line[0].x)
  14. dy = sign(line[1].y - line[0].y)
  15. x = line[0].x
  16. y = line[0].y
  17. while x != line[1].x or y != line[1].y:
  18. board[x][y] += 1
  19. x += dx
  20. y += dy
  21. board[x][y] += 1
  22. print(sum([1 for num in flatten(board) if num > 1]))