<jupyter_start><jupyter_text>The gravity assist was successful, and you're well on your way to the Venus refuelling station. During the rush back on Earth, the fuel management system wasn't completely installed, so that's next on the priority list.Opening the front panel reveals a jumble of wires. Specifically, two wires are connected to a central port and extend outward on a grid. You trace the path each wire takes as it leaves the central port, one wire per line of text (your puzzle input).The wires twist and turn, but the two wires occasionally cross paths. To fix the circuit, you need to find the intersection point closest to the central port. Because the wires are on a grid, use the Manhattan distance for this measurement. While the wires do technically cross right at the central port where they both start, this point does not count, nor does a wire count as crossing with itself.For example, if the first wire's path is R8,U5,L5,D3, then starting from the central port (o), it goes right 8, up 5, left 5, and finally down 3:.....................................+----+.....|....|.....|....|.....|....|..........|..o-------+............Then, if the second wire's path is U7,R6,D4,L4, it goes up 7, right 6, down 4, and left 4:............+-----+....|.....|....|..+--X-+..|..|..|.|..|.-X--+.|..|..|....|..|.......|..o-------+............These wires cross at two locations (marked X), but the lower-left one is closer to the central port: its distance is 3 + 3 = 6.Here are a few more examples:R75,D30,R83,U83,L12,D49,R71,U7,L72U62,R66,U55,R34,D71,R55,D58,R83 = distance 159R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = distance 135What is the Manhattan distance from the central port to the closest intersection?<jupyter_code># Examples
#path1 = 'R8,U5,L5,D3'
#path2 = 'U7,R6,D4,L4'

path1 = 'R75,D30,R83,U83,L12,D49,R71,U7,L72'
path2 = 'U62,R66,U55,R34,D71,R55,D58,R83'



#path1 = 'R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51'
#path2 = 'U98,R91,D20,R16,D67,R40,U7,R15,U6,R7'

route = {1:[(0,0)],2:[(0,0)]}
path1 = path1.split(',')
path2 = path2.split(',')
# Puzzle input

paths = []
with open('input.txt') as f:
    for line in f:
        paths.append(line)

path1 = paths[0].split(',')
path2 = paths[1].split(',')

def left(value,key):
    
    for i in range(value):
        x,y = route[key][-1]

        x1 = x-1

        route[key].append((x1,y))
    
def right(value,key):
    
    for i in range(value):
        x,y = route[key][-1]

        x1 = x+1

        route[key].append((x1,y))
    
def up(value,key):
    
    for i in range(value):
        x,y = route[key][-1]

        y1 = y+1

        route[key].append((x,y1))
    
def down(value,key):
    
    for i in range(value):
        x,y = route[key][-1]

        y1 = y-1

        route[key].append((x,y1))

direction = {
            'R':right,
            'L':left,
            'U':up,
            'D':down,
}
route = {1:[(0,0)],2:[(0,0)]}
for step in path1:
    direction[step[0]](int(step[1:]),1)
    
for step in path2:
    direction[step[0]](int(step[1:]),2)


crosses = set(route[1]).intersection(set(route[2]))
crosses

distances = []
for x,y in crosses:
    distances.append(abs(x)+abs(y))
distances.remove(0)
print(min(distances))<jupyter_output>865<jupyter_text>--- 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,L72U62,R66,U55,R34,D71,R55,D58,R83 = 610 stepsR98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = 410 stepsWhat is the fewest combined steps the wires must take to reach an intersection?<jupyter_code>crosses
cross_steps = []
for cross in crosses:
    a = route[1].index(cross)
    b = route[2].index(cross)
    
    cross_steps.append(a + b)
cross_steps.remove(0)
min(cross_steps)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17278442506590955, 'fraction_numerical': 0.07057392009734334, 'mean_word_length': 3.1585160202360876, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '606687', 'license': 'MIT', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1712, 'n_words': 558, 'path': 'Day 03/Day_03_part_1_and_2.ipynb', 'repo_name': 'gnfrazier/AdventofCode19', 'size': '4931'}