Day 7 (possibility to disable dynamic programming)
This commit is contained in:
@@ -1,15 +1,18 @@
|
|||||||
from day07.common import BaseGraph
|
from day07.common import BaseGraph
|
||||||
|
import datetime
|
||||||
|
|
||||||
class Graph(BaseGraph):
|
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 = []
|
visited = []
|
||||||
|
|
||||||
def count_bags(nxt):
|
def count_bags(nxt):
|
||||||
if nxt not in self._nodes:
|
if nxt not in self._nodes:
|
||||||
return 0
|
return 0
|
||||||
node = self._nodes[nxt]
|
node = self._nodes[nxt]
|
||||||
|
if not use_cache:
|
||||||
|
node.visited = False
|
||||||
|
node.count = 0
|
||||||
if node.visited:
|
if node.visited:
|
||||||
return node.count
|
return node.count
|
||||||
node.visited = True
|
node.visited = True
|
||||||
@@ -33,6 +36,13 @@ count = -1
|
|||||||
for line in lines:
|
for line in lines:
|
||||||
graph.add_line(line)
|
graph.add_line(line)
|
||||||
|
|
||||||
|
a = datetime.datetime.now()
|
||||||
count = graph.depth_first_search("shiny gold", search_contained_by=False) - 1
|
count = graph.depth_first_search("shiny gold", search_contained_by=False) - 1
|
||||||
|
print("With dyn programming", datetime.datetime.now() - a)
|
||||||
print(count)
|
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)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user