<reponame>matteodelabre/advent-of-code<filename>2021/05-2.py
lines = []

try:
    while True:
        lines.append(tuple(map(
            lambda x: tuple(map(int, x.split(","))),
            input().split(" -> ")
        )))
except EOFError:
    pass

w = max(max(line[0][0], line[1][0]) + 1 for line in lines)
h = max(max(line[0][1], line[1][1]) + 1 for line in lines)
grid = [[0] * w for i in range(h)]

for line in lines:
    dirx = 1 if line[0][0] < line[1][0] else -1
    diry = 1 if line[0][1] < line[1][1] else -1

    if line[0][0] == line[1][0]:
        for y in range(line[0][1], line[1][1] + diry, diry):
            grid[y][line[0][0]] += 1
    elif line[0][1] == line[1][1]:
        for x in range(line[0][0], line[1][0] + dirx, dirx):
            grid[line[0][1]][x] += 1
    else:
        for x, y in zip(
            range(line[0][0], line[1][0] + dirx, dirx),
            range(line[0][1], line[1][1] + diry, diry)
        ):
            grid[y][x] += 1

print(sum(x > 1 for line in grid for x in line))
