version
.start
to end
such that nodes named with lowercase letters are visited once, and nodes with uppercase letters can be visited any number of times.Part 1: A network of nodes is defined by a list of lines formatted as such:
2 <-> 0, 3, 4
> In this case, node `2` is connected to `0`, `3`, and `4` and vice versa.
> How many nodes are in the group that contains the node `0`?
First, load the data into an [[wiki:adjacency map]]():
```python
nodes = set()
neighbors = collections.defaultdict(set)
for line in lib.input():
source, destinations = line.split('<->')
source = int(source.strip())
nodes.add(source)
for destination in destinations.strip().split(','):
destination = int(destination.strip())
nodes.add(destination)
neighbors[source].add(destination)
neighbors[destination].add(source)
Then, write a function that can take a node and recursively expand until it finds all nodes in the same group:
Part 1: A tree is defined as such:
node (weight) -> child1, child2, ...
node (weight)
Where a
node
always has a weight, but may or may not have child nodes.
What is the name of the root
node
of the tree (the node without a parent)?