Skip to content

Commit

Permalink
Optimize day 23 a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
aastrand committed Dec 23, 2023
1 parent 9913bab commit 31edc7a
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions day23/day23.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,25 @@ def simplify(graph, dist):
for r in removals:
del graph[r]

return graph, dist
# unncessary but faster: use integers in a matrix instead of tuples in a dict
lookup = {}
for i, node in enumerate(graph):
lookup[node] = i

listgraph = []
for i, node in enumerate(graph):
listgraph.append([])
listgraph[i] = [lookup[n] for n in graph[node]]

listdist = []
for i, node in enumerate(graph):
listdist.append([0] * len(graph))

for node in graph:
for neighbor in graph[node]:
listdist[lookup[node]][lookup[neighbor]] = dist[(node, neighbor)]

return listgraph, listdist


def find_longest_path(graph, dist, start, end):
Expand All @@ -135,7 +153,7 @@ def dfs(node, visited):

max_length = float("-inf")
for neighbor in graph[node]:
length = dist[(node, neighbor)] + dfs(neighbor, visited)
length = dist[node][neighbor] + dfs(neighbor, visited)
max_length = max(max_length, length)

visited.remove(node)
Expand All @@ -154,18 +172,18 @@ def visitor(graph, pos, val):

grid.walk(visitor)

start = (1, 0)
end = (grid.maxX - 1, grid.maxY)
graph = from_grid(grid, lambda grid, pos, val, other: other == "." and val == ".")

dist = {}
for node in graph:
for n in graph[node]:
dist[(node, n)] = 1

# shrink the graph to the points that matter - the junctions where path split
graph, dist = simplify(graph, dist)

return find_longest_path(graph, dist, start, end)
# brute force find all paths with dfs
return find_longest_path(graph, dist, 0, len(graph) - 1)


def main():
Expand Down

0 comments on commit 31edc7a

Please sign in to comment.