Solution to problem 5 part 2 in Python

This commit is contained in:
David Doblas Jiménez 2022-02-04 13:57:00 +01:00
parent b26e52c4ea
commit 4d5681a0be

View File

@ -59,7 +59,7 @@
from collections import defaultdict
from typing import DefaultDict, Tuple
with open("files/test.txt") as f:
with open("files/P5.txt") as f:
points = [
points
for line in f.read().strip().split("\n")
@ -139,6 +139,7 @@ def part_1() -> None:
def part_2() -> None:
grid: DefaultDict[Tuple[int, int], int] = defaultdict(int)
for idx in range(len(points) // 2):
# print(idx)
if x_1[idx] == x_2[idx]:
start, stop, step = get_range(y_1[idx], y_2[idx])
for pos in range(start, stop, step):
@ -148,17 +149,16 @@ def part_2() -> None:
for pos in range(start, stop, step):
grid[pos, y_1[idx]] += 1
elif abs(x_1[idx] - x_2[idx]) == abs(y_1[idx] - y_2[idx]):
if x_2[idx] > x_1[idx]:
start_x, stop_x, step_x = get_range(x_1[idx], x_2[idx])
start_y, stop_y, step_y = get_range(y_1[idx], y_2[idx])
pos_x = [pos_x for pos_x in range(start_x, stop_x, step_x)]
pos_y = [pos_y for pos_y in range(start_y, stop_y, step_y)]
for px, py in zip(pos_x, pos_y):
grid[px, py] += 1
start_x, stop_x, step_x = get_range(x_1[idx], x_2[idx])
start_y, stop_y, step_y = get_range(y_1[idx], y_2[idx])
pos_x = [pos_x for pos_x in range(start_x, stop_x, step_x)]
pos_y = [pos_y for pos_y in range(start_y, stop_y, step_y)]
for px, py in zip(pos_x, pos_y):
grid[px, py] += 1
print(sum(1 for key in grid.values() if key > 1))
if __name__ == "__main__":
# part_1()
part_1()
part_2()