You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

import numpy as np
file = "inputs/day5.input"
numbers = open(file, "rb").readlines()
numbers = [x.decode().replace("\n", "").split(" ") for x in numbers]
start_x = [int(x[0].split(",")[0]) for x in numbers]
end_x = [int(x[2].split(",")[0]) for x in numbers]
start_y = [int(x[0].split(",")[1]) for x in numbers]
end_y = [int(x[2].split(",")[1]) for x in numbers]
max_val = max(max(end_x), max(end_y))
vents = np.zeros((max_val + 1, max_val + 1))
def draw_diagonal(mat, x1, x2, y1, y2):
length = abs(x2 - x1)
if x1 < x2:
xincr = 1
else:
xincr = -1
if y1 < y2:
yincr = 1
else:
yincr = -1
for i in range(length + 1):
mat[y1 + i * yincr, x1 + i * xincr] += 1
return mat
for i in range(len(start_x)):
if start_y[i] < end_y[i]:
y1 = start_y[i]
y2 = end_y[i]
else:
y1 = end_y[i]
y2 = start_y[i]
if start_x[i] < end_x[i]:
x1 = start_x[i]
x2 = end_x[i]
else:
x1 = end_x[i]
x2 = start_x[i]
if start_x[i] == end_x[i]:
vents[y1 : y2 + 1, start_x[i]] += 1
elif start_y[i] == end_y[i]:
vents[start_y[i], x1 : x2 + 1] += 1
else:
vents = draw_diagonal(vents, start_x[i], end_x[i], start_y[i], end_y[i])
print(len(vents[vents >= 2]))