graph solution, still running
This commit is contained in:
parent
2aeb55a5f4
commit
c2601d433a
|
@ -28,27 +28,6 @@ def solve(kart, pos, dist):
|
|||
nextpos = add(pos, d)
|
||||
q += [(newkart, nextpos, dist + 1), ]
|
||||
|
||||
def solve2(kart, pos, dist):
|
||||
#ic(pos, dist)
|
||||
global best, q, longest100
|
||||
if dist - dist % 100 > longest100:
|
||||
longest100 = dist - dist % 100
|
||||
ic(longest100)
|
||||
if pos == fin:
|
||||
if dist > best:
|
||||
best = dist
|
||||
ic(best)
|
||||
return
|
||||
if not (pos[0] >= 0 and pos[1] >= 0 and pos[0] < h and pos[1] < w):
|
||||
return
|
||||
if not kart[pos]:
|
||||
return
|
||||
newkart = kart.copy()
|
||||
newkart[pos] = False
|
||||
for d in DIRS:
|
||||
nextpos = add(pos, d)
|
||||
q += [(newkart, nextpos, dist + 1), ]
|
||||
|
||||
kart = np.asarray([[c for c in l] for l in lines(open(0))])
|
||||
h, w = kart.shape
|
||||
beg, fin = [(i, np.argmax(kart[i] == '.')) for i in (0, h - 1)]
|
||||
|
@ -63,9 +42,51 @@ while q:
|
|||
|
||||
print(best)
|
||||
|
||||
q += [(kart != '#', beg, 0)]
|
||||
vertices = [beg, fin]
|
||||
edges = defaultdict(lambda: set())
|
||||
|
||||
def find_vertices(kart, pos, dist, prev_vertex):
|
||||
global vertices, edges, q
|
||||
newkart = kart.copy()
|
||||
newkart[pos] = False
|
||||
newposes = []
|
||||
for d in DIRS:
|
||||
nextpos = add(pos, d)
|
||||
if not (nextpos[0] >= 0 and nextpos[1] >= 0 and nextpos[0] < h and nextpos[1] < w):
|
||||
continue
|
||||
if not kart[nextpos]:
|
||||
continue
|
||||
newposes += [nextpos, ]
|
||||
if len(newposes) > 1: # we're on a new(?) vertex
|
||||
if pos not in vertices:
|
||||
vertices += [pos, ]
|
||||
for p in newposes:
|
||||
q += [(newkart, p, 1, pos), ]
|
||||
edges[prev_vertex] |= {(pos, dist)}
|
||||
edges[pos] |= {(prev_vertex, dist)}
|
||||
elif len(newposes) == 1:
|
||||
q += [(newkart, newposes[0], dist + 1, prev_vertex), ]
|
||||
elif pos == fin:
|
||||
edges[prev_vertex] |= {(pos, dist)}
|
||||
edges[pos] |= {(prev_vertex, dist)}
|
||||
|
||||
def lengths(fro, edges, to):
|
||||
if fro == to:
|
||||
return [0, ]
|
||||
acc = []
|
||||
for e in edges[fro]:
|
||||
newegg = deepcopy(edges)
|
||||
newegg[e[0]].remove((fro, e[1]))
|
||||
newegg[fro].remove(e)
|
||||
for l in lengths(e[0], newegg, fin):
|
||||
acc += [e[1] + l, ]
|
||||
return acc
|
||||
|
||||
q += [(kart != '#', beg, 0, beg), ]
|
||||
while q:
|
||||
solve2(*q.pop())
|
||||
find_vertices(*q.popleft())
|
||||
|
||||
ic(edges)
|
||||
|
||||
print(max(lengths(beg, edges, fin)))
|
||||
|
||||
print(best)
|
||||
|
|
Loading…
Reference in New Issue