[WIP] Solution to problem 3 part 2 in Python

This commit is contained in:
David Doblas Jiménez 2022-02-10 20:22:52 +01:00
parent e9ac94cfa0
commit 14955c5bb9

View File

@ -62,7 +62,11 @@ from typing import List
from sympy import Segment
<<<<<<< HEAD
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()]
@ -111,5 +115,110 @@ 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
# minimize the signal delay.
# To do this, calculate the number of steps each wire takes to reach each
# intersection; choose the intersection where the sum of both wires' steps is
# lowest. If a wire visits a position on the grid multiple times, use the steps
# value from the first time it visits that position when calculating the total
# value of a specific intersection.
# The number of steps a wire takes is the total number of grid squares the wire
# has entered to get to that location, including the intersection being
# considered. Again consider the example from above:
# ...........
# .+-----+...
# .|.....|...
# .|..+--X-+.
# .|..|..|.|.
# .|.-X--+.|.
# .|..|....|.
# .|.......|.
# .o-------+.
# ...........
# In the above example, the intersection closest to the central port is reached
# after 8+5+5+2 = 20 steps by the first wire and 7+6+4+3 = 20 steps by the
# second wire for a total of 20+20 = 40 steps.
# However, the top-right intersection is better: the first wire takes only
# 8+5+2 = 15 and the second wire takes only 7+6+2 = 15, a total of 15+15 = 30
# steps.
# Here are the best steps for the extra examples from above:
# R75,D30,R83,U83,L12,D49,R71,U7,L72
# U62,R66,U55,R34,D71,R55,D58,R83 = 610 steps
# R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
# U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = 410 steps
# What is the fewest combined steps the wires must take to reach an
# intersection?
print(path_a)
print(path_b)
def accumulate_path(wire):
path = 0
for step in wire:
_, length = step[0], int(step[1:])
path += length
return path
def get_direction(segment):
is_horizontal = False
if segment.points[0][0] == segment.points[1][0]:
is_horizontal = True
if is_horizontal:
if segment.points[0][0] > segment.points[1][0]:
direction = "L"
else:
direction = "R"
return is_horizontal, direction
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)
def part_2() -> None:
closest = inf
for idx_a, (p1, p2) in enumerate(zip(path_a[:-1], path_a[1:]), start=1):
s1 = Segment(p1, p2)
for idx_b, (p3, p4) in enumerate(
zip(path_b[:-1], path_b[1:]), start=1
):
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])
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_2()
>>>>>>> ef8582f ([WIP] Solution to problem 3 part 2 in Python)