-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20backup.py
202 lines (151 loc) · 4.39 KB
/
day20backup.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import sys
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
walls = []
start = None
end = None
max_x = 0
max_y = 0
with open(inputfile,"r") as input1:
for i,line in enumerate(input1):
line = line.rstrip()
for j,val in enumerate(line):
if val == "#":
walls.append((i,j))
elif val == "S":
start = (i,j)
elif val == "E":
end = (i,j)
if j > max_y:
max_y = j
max_x = i
def find_path():
global start, end, walls, max_x, max_y
where = start
path = []
while not where == end:
(x,y) = where
for step in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
if ((0 <= step[0] <= max_x) and
(0 <= step[1] <= max_y) and
(not step == start) and
(not step in path) and
(not step in walls)):
path.append(step)
where = step
break
return path
def find_shortcuts(where):
global walls, max_x, max_y, start
options = []
(x,y) = where
for stepone in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
if stepone in walls:
(x1,y1) = stepone
for steptwo in [(x1+1,y1),(x1-1,y1),(x1,y1+1),(x1,y1-1)]:
if ((0 <= steptwo[0] <= max_x) and
(0 <= steptwo[1] <= max_y) and
(not steptwo == start) and
(not steptwo == where) and
(not steptwo in walls)):
options.append(steptwo)
return options
def find_shortcuts2(fr,steps,path):
global walls, max_x, max_y, start,time
#print("Check "+str(fr)+" ("+str(steps)+") "+str(path))
options = set()
(x,y) = fr
if steps == time:
return options
for step in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
if steps == 0:
if step in walls:
options.update(find_shortcuts2(step,steps+1,path+[fr]))
elif ((0 <= step[0] <= max_x) and
(0 <= step[1] <= max_y) and
(not step == start) and
(not step in path)):
if not step in walls:
options.add(step)
options.update(find_shortcuts2(step,steps+1,path+[fr]))
return options
def find_shortcuts3(fr):
global walls, max_x, max_y, start, time
#print("Check "+str(fr)+" ("+str(steps)+") "+str(path))
options = set()
(x,y) = fr
for stepone in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
if stepone in walls:
(x1,y1) = stepone
for i in range(0,time-1):
for j in range(0,time-1):
if i+j <= time-1:
#print(str(i)+"+"+str(j)+"<="+str(time))
for step in [ (x1+i,y1+j), (x+i,y-j), (x-i,y+j), (x-i,y-j)]:
if ((0 <= step[0] <= max_x) and
(0 <= step[1] <= max_y) and
(not step == start)):
options.add((step,i+j+1))
return options
def eval_shortcuts():
global savehowmuch, path
shortcuts = []
for i,field in enumerate(path):
options = find_shortcuts(field)
for o in options:
print(field)
print(o)
print(path[i+savehowmuch+1:])
if o in path[i+savehowmuch+1:]:
print("Can cut from "+str(field)+" to "+str(o))
#print(path)
#print(path[i+savehowmuch+1:])
shortcuts.append((field,o))
return shortcuts
def eval_shortcuts2():
global path
shortcuts = {}
for i,field in enumerate(path):
#print(i)
options = find_shortcuts3(field)
#print(str(field)+" - "+str(options))
for o in options:
if o[0] in path[i+1:]:
print("Can cut from "+str(field)+" to "+str(o))
fullpath = path[:path.index(field)]
fullpath = fullpath + path[path.index(o[0])+1:]
#print(path)
print(fullpath)
#print(len(fullpath)+o[1])
if (not (field,o[0]) in shortcuts) or len(fullpath)+o[1] < shortcuts[(field,o[0])]:
shortcuts[(field,o[0])] = len(fullpath)+o[1]
return shortcuts
savehowmuch = 2
time = 2
path = find_path()
path.insert(0,start)
print(path)
print(len(path))
def run_part1():
shortcuts = eval_shortcuts()
print(len(shortcuts))
def run_part2():
global savehowmuch, time
savehowmuch = 72
time = 20
shortcuts = eval_shortcuts2()
result = [shortcuts[x] for x in shortcuts if len(path) - shortcuts[x] >= savehowmuch]
print(shortcuts)
print(result)
count = {}
for r in result:
if r in count:
count[r] += 1
else:
count[r] = 1
print(len(result))
run_part1()
#run_part2()