[WIP] Solve test 1 for problem 3 part 2

This commit is contained in:
David Doblas Jiménez 2022-02-10 22:23:45 +01:00
parent 14955c5bb9
commit b12e0632af

View File

@ -60,13 +60,10 @@
from math import inf
from typing import List
from sympy import Segment
from sympy import Segment, Point
<<<<<<< HEAD
with open("files/P3.txt") as f:
=======
# with open("files/P3.txt") as f:
with open("files/test1.txt") as f:
>>>>>>> ef8582f ([WIP] Solution to problem 3 part 2 in Python)
wire_a, wire_b = [path.split(",") for path in f.read().strip().split()]
@ -115,10 +112,6 @@ def part_1() -> None:
print(f"The manhattan distance to the closest intersection is {closest}")
<<<<<<< HEAD
if __name__ == "__main__":
part_1()
=======
# --- Part Two ---
# It turns out that this circuit is very timing-sensitive; you actually need to
@ -162,21 +155,24 @@ if __name__ == "__main__":
# What is the fewest combined steps the wires must take to reach an
# intersection?
print(path_a)
print(path_b)
# print(path_a)
# print(path_b)
def accumulate_path(wire):
def accumulate_path(wire, steps):
path = 0
for step in wire:
for step in wire[:steps]:
_, length = step[0], int(step[1:])
path += length
return path
# print(accumulate_path(wire_a, 2))
def get_direction(segment):
is_horizontal = False
if segment.points[0][0] == segment.points[1][0]:
if segment.points[0][1] == segment.points[1][1]:
is_horizontal = True
if is_horizontal:
@ -184,13 +180,49 @@ def get_direction(segment):
direction = "L"
else:
direction = "R"
else:
if segment.points[0][1] > segment.points[1][1]:
direction = "D"
else:
direction = "U"
return is_horizontal, direction
def get_range(segment, is_horizontal, direction):
if is_horizontal:
if direction == "L":
return range(segment.points[0][0], segment.points[1][0], -1)
else:
return range(segment.points[0][0], segment.points[1][0])
else:
if direction == "D":
return range(segment.points[0][1], segment.points[1][1], -1)
else:
return range(segment.points[0][1], segment.points[1][1])
def find_intersection(segment_a, segment_b, intersection_point):
horizontal_a, direction_a = get_direction(segment_a)
horizontal_b, direction_b = get_direction(segment_b)
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
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}")
steps = idx_a + idx_b
print(steps)
return steps
def part_2() -> None:
@ -203,14 +235,15 @@ def part_2() -> None:
s2 = Segment(p3, p4)
intersect = s1.intersection(s2)
if intersect:
print(intersect)
# manhattan_distance = abs(intersect[0][0]) + abs(
# intersect[0][1]
# )
print(path_a[idx_a], path_b[idx_b])
sum_of_steps = idx_a + idx_b
print(sum_of_steps)
print(s2, s2.points, s2.points[0], s2.points[0][0])
# 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)
@ -221,4 +254,3 @@ def part_2() -> None:
if __name__ == "__main__":
# part_1()
part_2()
>>>>>>> ef8582f ([WIP] Solution to problem 3 part 2 in Python)