aoc2021/d05/d05_2.py

26 lines
496 B
Python
Raw Normal View History

2021-12-07 23:42:19 +00:00
import sys
W,H = 1000,1000
grid = [ [ 0 for _ in range(W) ] for _ in range(H) ]
L = eval(sys.stdin.read())
sign = lambda x: (1, -1)[x<0]
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 L:
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:
r = abs(x1-x2)
i = sign(x2-x1)
j = sign(y2-y1)
for a in range(r+1):
incr(x1+a*i,y1+a*j)
print(n)