-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
72 lines (54 loc) · 1.92 KB
/
day3.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
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_wires_from_file(file_path=top_dir + "resources/year2019_day3_input.txt"):
with open(file_path) as f:
return [l.strip() for l in f]
directions = {
"R": (1, 0),
"L": (-1, 0),
"U": (0, 1),
"D": (0, -1),
}
def get_wire_path(wire):
x, y = 0, 0
for instruction in wire.split(","):
(dx, dy), value = directions[instruction[0]], int(instruction[1:])
for _ in range(value):
x += dx
y += dy
yield x, y
def get_crossing_min_distance(wires):
crossings = set.intersection(*(set(get_wire_path(w)) for w in wires))
distances = [abs(x) + abs(y) for x, y in crossings]
return min(distances)
def get_crossing_min_delay(wires):
paths = dict()
for w in wires:
first_visit = dict()
for step, pos in enumerate(get_wire_path(w)):
if pos not in first_visit:
first_visit[pos] = step
for pos, step in first_visit.items():
paths.setdefault(pos, []).append(step)
crossings = [steps for steps in paths.values() if len(steps) == 2]
steps = [sum(steps) + 2 for steps in crossings]
return min(steps)
def run_tests():
wires = ["R8,U5,L5,D3", "U7,R6,D4,L4"]
assert get_crossing_min_distance(wires) == 6
assert get_crossing_min_delay(wires) == 30
wires = ["R75,D30,R83,U83,L12,D49,R71,U7,L72", "U62,R66,U55,R34,D71,R55,D58,R83"]
assert get_crossing_min_distance(wires) == 159
assert get_crossing_min_delay(wires) == 610
def get_solutions():
wires = get_wires_from_file()
print(get_crossing_min_distance(wires) == 1084)
print(get_crossing_min_delay(wires) == 9240)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)