You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.8 KiB
80 lines
1.8 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
from copy import deepcopy
|
|
|
|
|
|
def dijkstra(graph, s):
|
|
a = {}
|
|
v = {}
|
|
Q = initG(graph, s, a, v)
|
|
lq = len(Q)
|
|
while len(Q) > 0:
|
|
u = min([u for u in Q], key=lambda x: a[x])
|
|
Q.remove(u)
|
|
for vo in nb(*u):
|
|
if vo in Q:
|
|
distance_update(graph, u,vo,a,v)
|
|
return v
|
|
|
|
def initG(graph, s, a, v):
|
|
for vo in graph.keys():
|
|
a[vo] = 9999999999999999
|
|
v[vo] = 'null'
|
|
a[s] = 0
|
|
Q = [n for n in graph.keys()]
|
|
return Q
|
|
|
|
def distance_update(g, u,vo,a,v):
|
|
alt = a[u] + g[u]
|
|
if alt < a[vo]:
|
|
a[vo] = alt
|
|
v[vo] = u
|
|
|
|
def shortest_path(g, d, v):
|
|
w = [d]
|
|
u = d
|
|
r = 0
|
|
while v[u] != 'null':
|
|
r += g[u]
|
|
u = v[u]
|
|
w.append(u)
|
|
return w.reverse(), r
|
|
|
|
|
|
if __name__ == '__main__':
|
|
size = 71 # 7 for example, 71 for real input
|
|
pos = (0,0)
|
|
end = (70,70) # 6,6 for example, 70,70 for real input
|
|
blocks = [(int(l) for l in line.strip('\n').split(',')) for line in open(sys.argv[1])]
|
|
|
|
def nb(sx, sy):
|
|
n = [(0,1), (1,0), (0,-1), (-1,0)]
|
|
return [(min(size,max(0,sx+x)), min(size,max(0,sy+y))) for x,y in n]
|
|
|
|
g1 = {(x,y) : 1 for x in range(size) for y in range(size)}
|
|
|
|
for x,y in blocks[:1024]:
|
|
g1.pop((x,y))
|
|
|
|
# challenge 1
|
|
v = dijkstra(g1, (0,0))
|
|
w, r = shortest_path(g1, end, v)
|
|
res1 = r
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
# i did some binary search to figure out the index would be
|
|
# between 2800 and 2900
|
|
for x,y in blocks[1024:2800]:
|
|
g1.pop((x,y))
|
|
for x,y in blocks[2800:]:
|
|
g1.pop((x,y))
|
|
g = deepcopy(g1)
|
|
v = dijkstra(g, (0,0))
|
|
w, r = shortest_path(g, end, v)
|
|
if r == 0:
|
|
res2 = str(x) + "," + str(y)
|
|
break
|
|
print(f"challenge 2:\n{res2}")
|
|
|
|
|