-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.py
204 lines (166 loc) · 5.05 KB
/
day16.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
203
204
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
endloc = 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":
endloc = (i,j)
if j > max_y:
max_y = j
max_x = i
def find_neighbors(start):
global max_x, max_y, walls
(facing,x,y) = start
nbs = []
if 0 <= x-1 <= max_x and 0 <= y <= max_y and not (x-1,y) in walls:
nbs.append((x-1,y))
if 0 <= x+1 <= max_x and 0 <= y <= max_y and not (x+1,y) in walls:
nbs.append((x+1,y))
if 0 <= x <= max_x and 0 <= y-1 <= max_y and not (x,y-1) in walls:
nbs.append((x,y-1))
if 0 <= x <= max_x and 0 <= y+1 <= max_y and not (x,y+1) in walls:
nbs.append((x,y+1))
return nbs
def find_current():
global unvisited, costs
lowest_val = -1
lowest = None
for x in unvisited:
if x in costs:
if lowest_val == -1 or costs[x] < lowest_val:
lowest_val = costs[x]
lowest = x
if lowest:
return lowest
else:
print("huh?")
def set_cost(where,what,fr):
global costs,bestpaths
# print("Cost "+str(where)+": "+str(what)+ " (from "+str(fr)+")")
if where in costs and costs[where] > what:
costs[where] = what
oldpaths = []
for old in bestpaths[fr]:
oldpaths.append(old.copy())
paths = []
for p in oldpaths:
p.add((where[1],where[2]))
paths.append(p)
bestpaths[where] = paths
# print("Better path: "+str(bestpaths[where]))
#input()
elif where in costs and costs[where] == what:
oldpaths = []
for old in bestpaths[fr]:
oldpaths.append(old.copy())
paths = []
for p in oldpaths:
p.add((where[1],where[2]))
paths.append(p)
bestpaths[where] = bestpaths[where] + paths
# print("Equal path: "+str(bestpaths[where]))
#input()
elif not where in costs:
costs[where] = what
oldpaths = []
for old in bestpaths[fr]:
oldpaths.append(old.copy())
# print("Old paths: "+ str(oldpaths))
paths = []
for p in oldpaths:
p.add((where[1],where[2]))
paths.append(p)
bestpaths[where] = paths
# print("New path: "+str(bestpaths[where]))
#input()
def calc_costs(fr,nbs):
global costs
(facing,x,y) = fr
for nb in nbs:
#print("Consider "+str(nb))
if facing == "^" and nb[0] == x-1 and nb[1] == y:
set_cost(("^",nb[0],nb[1]),1 + costs[fr],fr)
set_cost(("<",nb[0],nb[1]),1001 + costs[fr],fr)
set_cost(("v",nb[0],nb[1]),2001 + costs[fr],fr)
set_cost((">",nb[0],nb[1]),1001 + costs[fr],fr)
elif facing == "v" and nb[0] == x+1 and nb[1] == y:
set_cost(("^",nb[0],nb[1]),2001 + costs[fr],fr)
set_cost(("<",nb[0],nb[1]),1001 + costs[fr],fr)
set_cost(("v",nb[0],nb[1]),1 + costs[fr],fr)
set_cost((">",nb[0],nb[1]),1001 + costs[fr],fr)
elif facing == "<" and nb[0] == x and nb[1] == y-1:
set_cost(("^",nb[0],nb[1]),1001 + costs[fr],fr)
set_cost(("<",nb[0],nb[1]),1 + costs[fr],fr)
set_cost(("v",nb[0],nb[1]),1001 + costs[fr],fr)
set_cost((">",nb[0],nb[1]),2001 + costs[fr],fr)
elif facing == ">" and nb[0] == x and nb[1] == y+1:
set_cost(("^",nb[0],nb[1]),1001 + costs[fr],fr)
set_cost(("<",nb[0],nb[1]),2001 + costs[fr],fr)
set_cost(("v",nb[0],nb[1]),1001 + costs[fr],fr)
set_cost((">",nb[0],nb[1]),1 + costs[fr],fr)
# else:
# print("nope")
costs = {start: 0,
("^",start[1],start[2]): 1000,
("v",start[1],start[2]): 1000,
("<",start[1],start[2]): 2000}
visited = []
unvisited = [start]
bestpaths = {start: [{(start[1],start[2])}],
("^",start[1],start[2]): [{(start[1],start[2])}],
("v",start[1],start[2]): [{(start[1],start[2])}],
("<",start[1],start[2]): [{(start[1],start[2])}]
}
ends = []
for d in ["<","^",">","v"]:
ends.append((d,endloc[0],endloc[1]))
def run_part1():
global unvisited,costs, visited,bestpaths,ends
unvisited += ends
for x in range(max_x+1):
for y in range(max_y+1):
if not (x,y) in walls:
for d in ["<","^",">","v"]:
unvisited.append((d,x,y))
current = find_current()
while current:
if len(visited) % 1000 == 0:
print(len(visited))
if current in ends:
print("Found "+str(current))
break
#print("Processing "+str(current))
nbs = find_neighbors(current)
calc_costs(current,nbs)
#print(costs)
visited.append(current)
unvisited.remove(current)
current = find_current()
result = [costs[e] for e in ends]
print(result)
print(min(result))
def run_part2():
global bestpaths,ends
result = [bestpaths[e] for e in ends]
tiles = set()
for r in result:
for path in r:
for tile in path:
tiles.add(tile)
print(len(tiles))
run_part1()
run_part2()