Solution to problem 3 in Python
This commit is contained in:
parent
b12e0632af
commit
599830ed15
@ -62,8 +62,8 @@ from typing import List
|
||||
|
||||
from sympy import Segment, Point
|
||||
|
||||
# with open("files/P3.txt") as f:
|
||||
with open("files/test1.txt") as f:
|
||||
with open("files/P3.txt") as f:
|
||||
# with open("files/test3.txt") as f:
|
||||
wire_a, wire_b = [path.split(",") for path in f.read().strip().split()]
|
||||
|
||||
|
||||
@ -155,8 +155,6 @@ def part_1() -> None:
|
||||
|
||||
# What is the fewest combined steps the wires must take to reach an
|
||||
# intersection?
|
||||
# print(path_a)
|
||||
# print(path_b)
|
||||
|
||||
|
||||
def accumulate_path(wire, steps):
|
||||
@ -167,9 +165,6 @@ def accumulate_path(wire, steps):
|
||||
return path
|
||||
|
||||
|
||||
# print(accumulate_path(wire_a, 2))
|
||||
|
||||
|
||||
def get_direction(segment):
|
||||
is_horizontal = False
|
||||
if segment.points[0][1] == segment.points[1][1]:
|
||||
@ -204,24 +199,19 @@ def get_range(segment, is_horizontal, direction):
|
||||
|
||||
def find_intersection(segment_a, segment_b, intersection_point):
|
||||
is_horizontal_a, direction_a = get_direction(segment_a)
|
||||
# print(is_horizontal_a, direction_a)
|
||||
is_horizontal_b, direction_b = get_direction(segment_b)
|
||||
# print(is_horizontal_b, direction_b)
|
||||
|
||||
range_a = get_range(segment_a, is_horizontal_a, direction_a)
|
||||
# print(range_a)
|
||||
range_b = get_range(segment_b, is_horizontal_b, direction_b)
|
||||
# print(range_b)
|
||||
|
||||
# print(f"intersection point is {intersection_point}")
|
||||
# steps = 0
|
||||
steps = 0
|
||||
for idx_a, a in enumerate(range_a):
|
||||
for idx_b, b in enumerate(range_b):
|
||||
# print(Point(a, b))
|
||||
if Point(a, b) == intersection_point:
|
||||
print(f"intersection at {a,b}")
|
||||
if (
|
||||
Point(a, b) == intersection_point
|
||||
or Point(b, a) == intersection_point
|
||||
):
|
||||
steps = idx_a + idx_b
|
||||
print(steps)
|
||||
return steps
|
||||
|
||||
|
||||
@ -235,22 +225,16 @@ def part_2() -> None:
|
||||
s2 = Segment(p3, p4)
|
||||
intersect = s1.intersection(s2)
|
||||
if intersect:
|
||||
# print(intersect[0])
|
||||
# print(path_a[idx_a], path_b[idx_b])
|
||||
until_intersection = find_intersection(s1, s2, intersect[0])
|
||||
# sum_of_steps = idx_a + idx_b
|
||||
steps_a = accumulate_path(wire_a, idx_a)
|
||||
steps_b = accumulate_path(wire_b, idx_b)
|
||||
# print(sum_of_steps)
|
||||
# print(s1, s1.points, s1.points[0], s1.points[0][0])
|
||||
sum_of_steps = steps_a + steps_b + until_intersection
|
||||
if sum_of_steps < closest:
|
||||
closest = sum_of_steps
|
||||
# print(s1, s2)
|
||||
|
||||
print(f"The minimum steps to an intersection is {closest}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# part_1()
|
||||
part_1()
|
||||
part_2()
|
||||
|
Loading…
Reference in New Issue
Block a user