-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-1.py
More file actions
executable file
·159 lines (144 loc) · 4.76 KB
/
Copy path03-1.py
File metadata and controls
executable file
·159 lines (144 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
import input_day_three
def generateGrid(wires):
grid = []
horizontal_length = findMaxLength(wires) + 2
vertical_length = findMaxHeight(wires) + 2
for i in range(horizontal_length):
grid.append(['.'] * vertical_length)
return grid
def findMaxLength(wires):
max = 0
for i in wires[0]:
if i[0] == 'L' or i[0] == 'R':
if int(i[1]) > max:
max = int(i[1])
for i in wires[1]:
if i[0] == 'L' or i[0] == 'R':
if int(i[1]) > max:
max = int(i[1])
return max
def findMaxHeight(wires):
max = 0
for i in wires[0]:
if i[0] == 'U' or i[0] == 'D':
if int(i[1]) > max:
max = int(i[1])
for i in wires[1]:
if i[0] == 'U' or i[0] == 'D':
if int(i[1]) > max:
max = int(i[1])
return max
def parseInput(input):
input = input.split('\n')
input[0] = input[0].split(',')
input[1] = input[1].split(',')
return input
def calculateWiresTurns(wires):
wire0 = [ 0, 0 ]
wire1 = [ 0, 0 ]
wire0_positions = []
wire1_positions = []
for i in wires[0]:
if i[0] == 'U':
wire0[0] += int(i[1:])
wire0_positions.append(wire0.copy())
elif i[0] == 'D':
wire0[0] -= int(i[1:])
wire0_positions.append(wire0.copy())
elif i[0] == 'L':
wire0[1] -= int(i[1:])
wire0_positions.append(wire0.copy())
elif i[0] == 'R':
wire0[1] += int(i[1:])
wire0_positions.append(wire0.copy())
for i in wires[1]:
if i[0] == 'U':
wire1[0] += int(i[1:])
wire1_positions.append(wire1.copy())
elif i[0] == 'D':
wire1[0] -= int(i[1:])
wire1_positions.append(wire1.copy())
elif i[0] == 'L':
wire1[1] -= int(i[1:])
wire1_positions.append(wire1.copy())
elif i[0] == 'R':
wire1[1] += int(i[1:])
wire1_positions.append(wire1.copy())
return wire0_positions, wire1_positions
def calculateWiresPaths(wires):
wire0 = [ 0, 0 ]
wire0_positions = []
wire1 = [ 0, 0 ]
wire1_positions = []
wire_crossings = []
for i in wires[0]:
if i[0] == 'U':
for j in range(int(i[1:])):
wire0[0] += 1
wire0_positions.append(wire0.copy())
elif i[0] == 'D':
for j in range(int(i[1:])):
wire0[0] -= 1
wire0_positions.append(wire0.copy())
elif i[0] == 'L':
for j in range(int(i[1:])):
wire0[1] -= 1
wire0_positions.append(wire0.copy())
elif i[0] == 'R':
for j in range(int(i[1:])):
wire0[1] += 1
wire0_positions.append(wire0.copy())
for i in wires[1]:
if i[0] == 'U':
for j in range(int(i[1:])):
wire1[0] += 1
wire1_positions.append(wire1.copy())
if wire1.copy() in wire0_positions:
wire_crossings.append(wire1.copy())
elif i[0] == 'D':
for j in range(int(i[1:])):
wire1[0] -= 1
wire1_positions.append(wire1.copy())
if wire1.copy() in wire0_positions:
wire_crossings.append(wire1.copy())
elif i[0] == 'L':
for j in range(int(i[1:])):
wire1[1] -= 1
wire1_positions.append(wire1.copy())
if wire1.copy() in wire0_positions:
wire_crossings.append(wire1.copy())
elif i[0] == 'R':
for j in range(int(i[1:])):
wire1[1] += 1
wire1_positions.append(wire1.copy())
if wire1.copy() in wire0_positions:
wire_crossings.append(wire1.copy())
return wire0_positions, wire1_positions, wire_crossings
def calculateWiresCrossings(wire0, wire1):
crossings = []
for i in wire0:
if i in wire1:
crossings.append(i.copy())
return crossings
def findShortestDistance(crossings):
result = [ 10000000000, [0,0] ]
for i in crossings:
pos1 = abs(i[0])
pos2 = abs(i[1])
manhattan = pos1 + pos2
if manhattan < result[0]:
result[0] = manhattan
result[1] = i
# result = min([sum(i) for i in crossings])
return result
if __name__ == "__main__":
# wires = '''R8,U5,L5,D3
#U7,R6,D4,L4'''
# wires = parseInput(wires)
# wire0_turns, wire1_turns = calculateWiresTurns(wires)
# print(wire0_turns, wire1_turns)
wires = parseInput(input_day_three.input)
wire0, wire1, crossings = calculateWiresPaths(wires)
answer = findShortestDistance(crossings)
print(answer)