-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp03.py
34 lines (25 loc) · 895 Bytes
/
p03.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
from aocd import data
directions = {'U': 1j, 'D': -1j,
'L': -1, 'R': 1}
def lay_wire(layout):
point = 0j
steps = 0
for instruction in layout:
direction, amount = instruction[0], int(instruction[1:])
for _ in range(amount):
steps += 1
point += directions[direction]
yield point, steps
wire_a, wire_b = map(lambda wire: wire.split(','), data.splitlines())
points = {}
intersections = []
intersection_steps = []
for point, steps in lay_wire(wire_a):
points[point] = steps
for point, steps in lay_wire(wire_b):
if point in points:
intersection_steps.append(steps + points[point])
intersections.append(point)
point = min(intersections, key=lambda point: (abs(point.real), abs(point.imag)))
print('Part 1:', int(abs(point.real) + abs(point.imag)))
print('Part 2:', min(intersection_steps))