aoc2021/d05/d05_2.py

22 lines
470 B
Python

sign = lambda x: (1,-1)[x<0]
grid = [[0 for _ in range(1000)] for _ in range(1000)]
n=0
def incr(x,y):
global n
grid[x][y]+=1
if grid[x][y]==2:
n+=1
for ((x1,y1),(x2,y2)) in eval(__import__("sys").stdin.read()):
if x1 == x2:
for y in range(min(y1,y2), max(y1,y2)+1):
incr(x1,y)
elif y1 == y2:
for x in range(min(x1,x2), max(x1,x2)+1):
incr(x,y1)
else:
i = sign(x2-x1)
j = sign(y2-y1)
for a in range(abs(x1-x2)+1):
incr(x1+a*i,y1+a*j)
print(n)