Day 7
This commit is contained in:
35
day07/part1.py
Normal file
35
day07/part1.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from day07.common import BaseGraph
|
||||
|
||||
|
||||
class Graph(BaseGraph):
|
||||
def breadth_first_search(self, start: str, search_contained_by: bool = False):
|
||||
visited = []
|
||||
todo = [start]
|
||||
while len(todo) > 0:
|
||||
nxt = todo.pop()
|
||||
if nxt in self._nodes:
|
||||
node = self._nodes[nxt]
|
||||
if node.visited:
|
||||
continue
|
||||
node.visited = True
|
||||
visited.append(node)
|
||||
yield nxt
|
||||
iter_over = node.contained_by if search_contained_by else node.contains
|
||||
for key, val in iter_over.items():
|
||||
if val > 0:
|
||||
todo.append(key)
|
||||
for visit in visited:
|
||||
visit.visited = False
|
||||
|
||||
|
||||
graph = Graph()
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
count = -1
|
||||
|
||||
for line in lines:
|
||||
graph.add_line(line)
|
||||
|
||||
for found in graph.breadth_first_search("shiny gold", search_contained_by=True):
|
||||
count += 1
|
||||
|
||||
print(count)
|
||||
Reference in New Issue
Block a user