-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.py
169 lines (134 loc) · 4.92 KB
/
3.py
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
160
161
162
163
164
165
166
167
168
169
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return str((self.x, self.y))
def __hash__(self):
return hash((self.x, self.y))
def __eq__(self, other):
return (self.x, self.y) == (other.x, other.y)
def __ne__(self, other):
return not self.__eq__(other)
def manhattanDistance(self, point):
"""Returns the Manhattan distance to another point."""
return abs(self.x - point.x) + abs(self.y - point.y)
class WireSegment:
def __init__(self, starting_point, direction, distance):
assert direction in {"U", "D", "L", "R"}, "Invalid direction"
self.starting_point = starting_point
self.direction = direction
self.distance = distance
def __repr__(self):
return "WireSegment(" + str(self.starting_point) + ", " + self.direction + ", " + str(self.distance) + ")"
def isVertical(self):
"""Returns whether the WireSegment is aligned vertically."""
return (self.direction in {"U", "D"})
def isHorizontal(self):
"""Returns whether the WireSegment is aligned horizontally."""
return not self.isVertical()
def getStartingPoint(self):
"""Returns the starting point of the WireSegment."""
return self.starting_point
def getEndPoint(self):
"""Returns the ending point of the WireSegment."""
x = self.starting_point.x
y = self.starting_point.y
if self.direction is "L":
return Point(x - self.distance, y)
elif self.direction is "R":
return Point(x + self.distance, y)
elif self.direction is "U":
return Point(x, y + self.distance)
elif self.direction is "D":
return Point(x, y - self.distance)
def getPoints(self):
"""Returns all grid points touched by the WireSegment."""
start = self.starting_point
end = self.getEndPoint()
if self.direction is "L":
return {Point(x, start.y) for x in range(end.x, start.x + 1)}
elif self.direction is "R":
return {Point(x, start.y) for x in range(start.x, end.x + 1)}
elif self.direction is "U":
return {Point(start.x, y) for y in range(start.y, end.y + 1)}
elif self.direction is "D":
return {Point(start.x, y) for y in range(end.y, start.y + 1)}
def getIntersections(self, segment):
"""Checks against another WireSegment and returns the set of all intersections."""
a1 = self.getStartingPoint()
a2 = self.getEndPoint()
b1 = segment.getStartingPoint()
b2 = segment.getEndPoint()
if (
min(a1.x, a2.x) > max(b1.x, b2.x) or
max(a1.x, a2.x) < min(b1.x, b2.x) or
min(a1.y, a2.y) > max(b1.y, b2.y) or
max(a1.y, a2.y) < min(b1.y, b2.y)
):
return set()
else:
return self.getPoints().intersection(segment.getPoints())
def stepsToReachPoint(self, point):
"""Returns the number of steps along the WireSegment it takes to reach the given Point."""
assert point in self.getPoints()
return self.starting_point.manhattanDistance(point)
class Wire:
def __init__(self, segments):
self.segments = segments
self.starting_point = segments[0].starting_point
def isContinuous(self):
"""Returns whether all consecutive WireSegments of the Wire are connected."""
cursor = self.starting_point
for segment in self.segments:
if cursor == segment.getStartingPoint():
cursor = segment.getEndPoint()
else:
return False
return True
def getPoints(self):
"""Returns all grid points touched by the Wire."""
points = set()
for segment in self.segments:
points = points.union(segment.getPoints())
return points
def getIntersections(self, wire):
"""Checks against another Wire or WireSegment and returns the set of all intersections."""
return set.intersection(self.getPoints(), wire.getPoints())
def stepsToReachPoint(self, point):
"""Returns the number of steps along the Wire it takes to reach the given Point."""
steps = 0
for segment in self.segments:
if point in segment.getPoints():
steps += segment.stepsToReachPoint(point)
return steps
else:
steps += segment.distance
raise ValueError("Given point is not part of Wire.")
if __name__ == "__main__":
with open("input/3.input", "r") as file:
wires = []
for line in file:
wire_data = line.strip().split(",")
wire_data = [(x[0], x[1:]) for x in wire_data]
cursor = Point(0,0)
segments = []
for segment_data in wire_data:
direction = segment_data[0]
distance = int(segment_data[1])
segment = WireSegment(cursor, direction, distance)
segments.append(segment)
cursor = segment.getEndPoint()
wires.append(Wire(segments))
central_port = Point(0,0)
wire_a = wires[0]
wire_b = wires[1]
intersections = Wire.getIntersections(wire_a, wire_b)
intersections.remove(central_port)
# Part 1
closestDistance = min([central_port.manhattanDistance(point) for point in intersections])
print("PART 1 | central port <--> closest wire intersection:", closestDistance)
# Part 2
fewestCombinedSteps = min([wire_a.stepsToReachPoint(point) + wire_b.stepsToReachPoint(point) for point in intersections])
print("PART 2 | minimum combined step distance:", fewestCombinedSteps)