-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
184 lines (177 loc) · 5.57 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from pathlib import Path
from collections import defaultdict
inputFile = Path("inputs/input-03-01.txt")
with inputFile.open() as f:
data1 = f.readline().rstrip().split(",")
data2 = f.readline().rstrip().split(",")
# Part 1
# start at 0,0 and trace route of first line, recording coordinates in dict
trace = defaultdict(list)
x,y = 0,0
for route in data1:
xprev = x
yprev = y
if route.startswith('R'):
x += int(route[1:])
elif route.startswith('L'):
x -= int(route[1:])
elif route.startswith('U'):
y += int(route[1:])
elif route.startswith('D'):
y -= int(route[1:])
if abs(x - xprev) > 0:
if x < 0:
for i in range(xprev,x,-1):
trace[(i - 1, yprev)].append(1)
elif x > 0:
for i in range(xprev,x):
trace[(i + 1, yprev)].append(1)
elif abs(y - yprev) > 0:
if y < 0:
for i in range(yprev,y, -1):
trace[(xprev, i - 1)].append(1)
if y > 0:
for i in range(yprev,y):
trace[(xprev, i + 1)].append(1)
#print(xprev,yprev,x,y)
x,y,best = 0,0,0
for route in data2:
xprev = x
yprev = y
if route.startswith('R'):
x += int(route[1:])
elif route.startswith('L'):
x -= int(route[1:])
elif route.startswith('U'):
y += int(route[1:])
elif route.startswith('D'):
y -= int(route[1:])
if abs(x - xprev) > 0:
if x < 0:
for i in range(xprev,x,-1):
if 1 in trace[(i - 1, yprev)]:
dist = abs(i - 1) + abs(yprev)
if best == 0:
best = dist
elif dist < best:
best = dist
if x > 0:
for i in range(xprev,x):
if 1 in trace[(i + 1, yprev)]:
dist = abs(i + 1) + abs(yprev)
if best == 0:
best = dist
elif dist < best:
best = dist
elif abs(y - yprev) > 0:
if y < 0:
for i in range(yprev,y, -1):
if 1 in trace[(xprev, i - 1)]:
dist = abs(xprev) + abs(i - 1)
if best == 0:
best = dist
elif dist < best:
best = dist
if y > 0:
for i in range(yprev,y):
if 1 in trace[(xprev, i + 1)]:
dist = abs(xprev) + abs( i + 1)
if best == 0:
best = dist
elif dist < best:
best = dist
print(best)
'''
# Part 2
# start at 0,0 and trace route of first line, recording coordinates in dict
trace = defaultdict(list)
x,y,steps = 0,0,0
for route in data1:
xprev = x
yprev = y
sprev = steps
if route.startswith('R'):
x += int(route[1:])
elif route.startswith('L'):
x -= int(route[1:])
elif route.startswith('U'):
y += int(route[1:])
elif route.startswith('D'):
y -= int(route[1:])
steps += int(route[1:])
if x - xprev < 0:
for i in range(xprev,x,-1):
trace[(i - 1, yprev)].append((1, sprev + xprev - i + 1))
elif x - xprev > 0:
for i in range(xprev,x):
trace[(i + 1, yprev)].append((1, sprev + i + 1 - xprev))
elif y - yprev < 0:
for i in range(yprev,y, -1):
trace[(xprev, i - 1)].append((1, sprev + yprev - i + 1))
elif y - yprev > 0:
for i in range(yprev,y):
trace[(xprev, i + 1)].append((1, sprev + i + 1 - yprev))
x,y,best,steps,add_steps = 0,0,0,0,0
for route in data2:
xprev = x
yprev = y
sprev = steps
if route.startswith('R'):
x += int(route[1:])
elif route.startswith('L'):
x -= int(route[1:])
elif route.startswith('U'):
y += int(route[1:])
elif route.startswith('D'):
y -= int(route[1:])
steps += int(route[1:])
if x-xprev < 0:
for i in range(xprev,x,-1):
try:
tup = trace[(i - 1, yprev)][0]
except IndexError:
continue
if tup[0] == 1:
add_steps = tup[1] + sprev + xprev - i + 1
if best == 0:
best = add_steps
elif add_steps < best:
best = add_steps
elif x-xprev > 0:
for i in range(xprev,x):
try:
tup = trace[(i + 1, yprev)][0]
except IndexError:
continue
if tup[0] == 1:
add_steps = tup[1] + sprev + i + 1 - xprev
if best == 0:
best = add_steps
elif add_steps < best:
best = add_steps
elif y-yprev < 0:
for i in range(yprev,y, -1):
try:
tup = trace[(xprev, i - 1)][0]
except IndexError:
continue
if tup[0] == 1:
add_steps = tup[1] + sprev + yprev - i + 1
if best == 0:
best = add_steps
elif add_steps < best:
best = add_steps
elif y-yprev > 0:
for i in range(yprev,y):
try:
tup = trace[(xprev, i + 1)][0]
except IndexError:
continue
if tup[0] == 1:
add_steps = tup[1] + sprev + i + 1 - yprev
if best == 0:
best = add_steps
elif add_steps < best:
best = add_steps
print(best)
'''