aoc2021/d05/d05_2.py

22 lines
470 B
Python
Raw Normal View History

2021-12-08 00:12:56 +00:00
sign = lambda x: (1,-1)[x<0]
grid = [[0 for _ in range(1000)] for _ in range(1000)]
2021-12-07 23:42:19 +00:00
n=0
def incr(x,y):
global n
grid[x][y]+=1
if grid[x][y]==2:
n+=1
2021-12-08 00:12:56 +00:00
for ((x1,y1),(x2,y2)) in eval(__import__("sys").stdin.read()):
2021-12-07 23:42:19 +00:00
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)
2021-12-08 00:12:56 +00:00
for a in range(abs(x1-x2)+1):
2021-12-07 23:42:19 +00:00
incr(x1+a*i,y1+a*j)
print(n)