2017 Day 12 Part 1
This commit is contained in:
parent
85f86147ee
commit
405f8bf533
1 changed files with 28 additions and 0 deletions
28
2017/day12.py
Normal file
28
2017/day12.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
with open("input/day12.txt") as f:
|
||||||
|
input = f.read()
|
||||||
|
|
||||||
|
neighbors = defaultdict(set)
|
||||||
|
|
||||||
|
for line in input.splitlines():
|
||||||
|
a, bs = line.split(" <-> ")
|
||||||
|
for b in bs.split(", "):
|
||||||
|
neighbors[a].add(b)
|
||||||
|
neighbors[b].add(a)
|
||||||
|
|
||||||
|
|
||||||
|
def connected(start, neighbors) -> int:
|
||||||
|
queue = [start]
|
||||||
|
seen = set()
|
||||||
|
while queue:
|
||||||
|
node = queue.pop(0)
|
||||||
|
if node in seen:
|
||||||
|
continue
|
||||||
|
|
||||||
|
seen.add(node)
|
||||||
|
queue.extend(neighbors[node])
|
||||||
|
return len(seen)
|
||||||
|
|
||||||
|
|
||||||
|
print(connected("0", neighbors))
|
||||||
Loading…
Reference in a new issue