diff --git a/day07/part2.py b/day07/part2.py index 68cec45..aa53649 100644 --- a/day07/part2.py +++ b/day07/part2.py @@ -1,15 +1,18 @@ from day07.common import BaseGraph - +import datetime class Graph(BaseGraph): - def depth_first_search(self, start: str, search_contained_by: bool = False): + def depth_first_search(self, start: str, search_contained_by: bool = False, use_cache=True): visited = [] def count_bags(nxt): if nxt not in self._nodes: return 0 node = self._nodes[nxt] + if not use_cache: + node.visited = False + node.count = 0 if node.visited: return node.count node.visited = True @@ -33,6 +36,13 @@ count = -1 for line in lines: graph.add_line(line) +a = datetime.datetime.now() count = graph.depth_first_search("shiny gold", search_contained_by=False) - 1 - +print("With dyn programming", datetime.datetime.now() - a) print(count) + +a = datetime.datetime.now() +count = graph.depth_first_search("shiny gold", search_contained_by=False, use_cache=False) - 1 +print("Without dyn programming", datetime.datetime.now() - a) +print(count) +