コード例 #1
0
 def test_case_5(self):
     graph = graph_builder.BuildGraphFromFile('testcase5.txt')
     expected_dist = 81
     finder = path_finder.PathFinder(graph)
     self.assertEqual(expected_dist, finder.FindShortestPath())
コード例 #2
0
 def test_go_and_absorb(self):
     graph = graph_builder.BuildGraphFromFile('testcase1.txt')
     expected = graph_builder.BuildGraph([['#', 'b', '.', 'A', '.', '.', '.', '@', '#']])
     path_finder.GoAndAbsorbNode(graph, 'a')
     self.assertEqual(expected, graph)
コード例 #3
0
ファイル: path_finder.py プロジェクト: BRapha/AoC_2019
        def ExploreNode(key, cost=0, graph=self.graph, keys=self.missing_keys):
            # if cost already exceeds lower cost option, we can stop
            if self.cost is not None and cost > self.cost:
                return

            # If node is a locked door, we can't proceed:
            if key.isupper() and key.lower() in keys:
                return

            if key != '@':
                cost += GoAndAbsorbNodeInGraph(key, graph)

            # remove key if it exists, because we've now found it
            keys.discard(key)

            if not keys:
                self.cost = cost if self.cost is None or cost < self.cost else self.cost
                return

            for neigh in graph['@'].keys():
                ExploreNode(neigh, cost, deepcopy(graph), keys.copy())

        ExploreNode('@')
        return self.cost


if __name__ == '__main__':
    graph_from_file = graph_builder.BuildGraphFromFile('input.txt')
    finder = PathFinder(graph_from_file)
    print(finder.FindShortestPath())