-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.py
40 lines (31 loc) · 963 Bytes
/
03.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
from lib import *
input = read_input(2019, 3)
def parse_wire(wire):
x = 0
y = 0
out = set()
for e in wire.split(","):
dx, dy = {"U": (0, -1), "D": (0, 1), "L": (-1, 0), "R": (1, 0)}[e[0]]
for _ in range(int(e[1:])):
x += dx
y += dy
out.add((x, y))
return out
wire1, wire2 = map(parse_wire, input.splitlines())
print(min(abs(x) + abs(y) for x, y in (wire1 & wire2)))
def parse_wire(wire):
x = 0
y = 0
out = {}
count = 0
for e in wire.split(","):
dx, dy = {"U": (0, -1), "D": (0, 1), "L": (-1, 0), "R": (1, 0)}[e[0]]
for _ in range(int(e[1:])):
x += dx
y += dy
count += 1
if (x, y) not in out:
out[(x, y)] = count
return out
wire1, wire2 = map(parse_wire, input.splitlines())
print(min(wire1[intersection] + wire2[intersection] for intersection in (wire1.keys() & wire2.keys())))