-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
90 lines (71 loc) · 1.95 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
with open('day3data.txt','r') as input_file:
payload1 = input_file.readline()
payload2 = input_file.readline()
payload1lines = payload1.split(',')
payload2lines = payload2.split(',')
'''
U is up
D is down
R is right
L is left
'''
def calc_line(payloadlines):
x=0
y=0
wire_length = 0
payloadpoints = set()
payloaddict = {}
for i in payloadlines:
x_vec= 0
y_vec= 0
if i[0] == 'U':
y_vec = 1
elif i[0] == 'D':
y_vec = -1
elif i[0] == 'R':
x_vec = 1
elif i[0] == 'L':
x_vec = -1
else:
raise ValueError
magnitude =int(i[1:])
# print(f"moving {magnitude} with x equalling {x_vec} and y equalling {y_vec}")
for _ in range(magnitude):
x += x_vec
y += y_vec
wire_length += 1
payloaddict[(x,y)] = wire_length
# print(f"current coordinates are {x},{y}")
return payloaddict
print('simulating Gibson routes')
payload1dict = calc_line(payload1lines)
payload2dict = calc_line(payload2lines)
print('Gibson routes simulated, hacking Gibson')
def mergeDict(dict1, dict2):
dict3 = {**dict1, **dict2}
for key, value in dict3.items():
if key in dict1 and key in dict2:
dict3[key] = [value , dict1[key]]
return dict3
overlay_dict = mergeDict(payload1dict, payload2dict)
search_space = len(overlay_dict)
search_index = 0.0
skinny_dict = {}
for key, value in overlay_dict.items():
search_index += 1
try:
if len(value) > 1:
skinny_dict[key] = value
else:
print(search_index/ search_space)
except TypeError:
pass
distance=9999999
distance_pair = 0
for key,value in skinny_dict.items():
trial_value = value[0] + value[1]
if trial_value < distance:
distance = trial_value
distance_pair = (key, value)
print(distance_pair)
print(distance)