def pisanski(G): GM = algorithms.isomorphism.GraphMatcher(G, G) paths_len = 0 for isomorph in GM.isomorphisms_iter(): for v1, v2 in isomorph.items(): paths_len += algorithms.shortest_path_length(G, v1, v2) return((paths_len*len(G))/(len(list(GM.isomorphisms_iter())) * 2))
def main() -> None: path_regex = next(get_data(today))[1:-1] build_map(path_regex) path_lengths = shortest_path_length(graph, Point(0, 0)).values() print(f'{today} star 1 = {max(path_lengths)}') print( f'{today} star 2 = {sum(1 for length in path_lengths if length >= 1000)}' )
def shortest_path_length(self,h1,h2): G = self._graph try: return nxa.shortest_path_length(G,h1,h2) except NetworkXNoPath: return 0
graph.add_edge((x2, y2), (x2 - 1, y2)) if grid[y2 - 1][x2] == '.': graph.add_edge((x2, y2), (x2, y2 - 1)) start_pos = portals['AA'][0] target_pos = portals['ZZ'][0] for portal, coords in portals.items(): # print(portal + ' ' + str(coords)) if len(coords) == 1: continue graph.add_edge(coords[0], coords[1]) print('Part 1: ' + str(shortest_path_length(graph, source=start_pos, target=target_pos))) graph = DiGraph() for level in range(30): portals = {} for y2 in range(h - 1): for x2 in range(w - 1): if grid[y2][x2] in letters: to_right = x2 + 2 == w or grid[y2][x2 + 2] == ' ' above = y2 + 2 == h or grid[y2 + 2][x2] == ' ' if not to_right: to_left = grid[y2][x2 + 2] == '.' if not above: under = grid[y2 + 2][x2] == '.'
def get_shortest_path_length(self, host): node = self.find_node_from_host(host) if node in self.graph.nodes(): return shortest_path_length(self.graph, node, '6LBR')
def getShortestPathLength(self,nodA,nodB): ''' Get the shortest path length between two nodes. ''' return alg.shortest_path_length(self.graph,nodA,nodB)
depth += 1 last_pos_stack.insert(0, (x, y)) elif curr == ')': depth -= 1 last_pos_stack = last_pos_stack[1:] elif curr == '|': x = last_pos_stack[0][0] y = last_pos_stack[0][1] parse(regex[1:], x, y, depth, last_pos_stack, graph) regex = '' with open('input.txt') as fp: for line in fp: regex += line[1:-1] graph = Graph() parse(regex, 0, 0, 0, [(0, 0)], graph) path_lengths = shortest_path_length(graph, source=(0, 0), target=None) print('part 1: ' + str(max(path_lengths.values()))) sum = 0 for len in path_lengths.values(): if len >= 1000: sum += 1 print('part 2: ' + str(sum))
def __init__(self, defstr): super().__init__() self.defstring = defstr self.build_graph() self.lengths = algorithms.shortest_path_length(self, Coord(0, 0))
for y in range(60): for x in range(60): if grid[y][x] == '.': dot_found = True if grid[y][x] == 'O' and (x, y) not in new_o_pos: if grid[y][x + 1] != '#' and grid[y][x + 1] != 'O': grid[y][x + 1] = 'O' new_o_pos.add((x + 1, y)) if grid[y][x - 1] != '#' and grid[y][x - 1] != 'O': grid[y][x - 1] = 'O' new_o_pos.add((x - 1, y)) if grid[y + 1][x] != '#' and grid[y + 1][x] != 'O': grid[y + 1][x] = 'O' new_o_pos.add((x, y + 1)) if grid[y - 1][x] != '#' and grid[y - 1][x] != 'O': grid[y - 1][x] = 'O' new_o_pos.add((x, y - 1)) for y2 in range(60): for x2 in range(60): print(grid[y2][x2], end='') print() i += 1 if not dot_found: break path_lengths = shortest_path_length(graph, source=(30, 30), target=target) print('Part 1: ' + str(path_lengths)) print('Part 2: ' + str(i))
def getShortestPathLength(self, nodA, nodB): ''' Get the shortest path length between two nodes. ''' return alg.shortest_path_length(self.graph, nodA, nodB)
def shortest_path_length(self, h1, h2): G = self._graph try: return nxa.shortest_path_length(G, h1, h2) except NetworkXNoPath: return 0