51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
|
#!/usr/bin/env python3
|
||
|
import fileinput
|
||
|
|
||
|
N = 1000
|
||
|
|
||
|
|
||
|
def main():
|
||
|
diag = [[0] * N for _ in range(N)]
|
||
|
|
||
|
vents = [
|
||
|
[
|
||
|
tuple(int(coord) for coord in coords.split(","))
|
||
|
for coords in str(line).split(" -> ")
|
||
|
]
|
||
|
for line in fileinput.input()
|
||
|
]
|
||
|
|
||
|
for (x1, y1), (x2, y2) in vents:
|
||
|
xsign = -1 if x2 < x1 else 1
|
||
|
ysign = -1 if y2 < y1 else 1
|
||
|
|
||
|
if x1 == x2:
|
||
|
# vertical line
|
||
|
for y in range(y1, y2 + ysign, ysign):
|
||
|
diag[y][x1] += 1
|
||
|
elif y1 == y2:
|
||
|
# horizontal line
|
||
|
for x in range(x1, x2 + xsign, xsign):
|
||
|
diag[y1][x] += 1
|
||
|
|
||
|
part1 = sum(point >= 2 for row in diag for point in row)
|
||
|
print("part 1:", part1)
|
||
|
|
||
|
for (x1, y1), (x2, y2) in vents:
|
||
|
xsign = -1 if x2 < x1 else 1
|
||
|
ysign = -1 if y2 < y1 else 1
|
||
|
|
||
|
if x1 != x2 and y1 != y2:
|
||
|
# diagonal line
|
||
|
y = y1
|
||
|
for x in range(x1, x2 + xsign, xsign):
|
||
|
diag[y][x] += 1
|
||
|
y += ysign
|
||
|
|
||
|
part2 = sum(point >= 2 for row in diag for point in row)
|
||
|
print("part 2:", part2)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|