/ 2021 / 5 / p2.py
p2.py
 1  from sys import stdin
 2  
 3  map = [[0 for _ in range(1000)] for _ in range(1000)]
 4  
 5  def signum(x):
 6      if x < 0: return -1
 7      if x > 0: return 1
 8      return 0
 9  
10  def point_range(x1, y1, x2, y2):
11      sx = signum(x2 - x1)
12      sy = signum(y2 - y1)
13      yield [x1, y1]
14      while x1 != x2 or y1 != y2:
15          x1 += sx
16          y1 += sy
17          yield [x1, y1]
18  
19  for line in stdin:
20      [[x1, y1], [x2, y2]] = [[int(x) for x in point.strip().split(',')] for point in line.split('->')]
21      for [x, y] in point_range(x1, y1, x2, y2):
22          map[x][y] += 1
23  
24  overlaps = len([cell
25      for column in map
26      for cell in column
27      if cell > 1])
28  
29  print(overlaps)