1
0
Fork 0
adventofcode/2017/12/pipes.py

30 lines
686 B
Python
Executable File

#!/usr/bin/env python3
import sys
def find_direct(node):
return set([t[1] for t in data if t[0] == node] +
[t[0] for t in data if t[1] == node])
def find_group(node):
group = {node}
new = {node}
while new:
next_new = set()
for item in new:
next_new.update(data[item])
new = next_new - group
group.update(next_new)
return group
lines = [line.strip().split(" <-> ") for line in sys.stdin.readlines()]
data = {int(l[0]): set(map(int, l[1].split(", "))) for l in lines}
del lines
groups, left = 0, set(data)
while left:
groups += 1
left -= find_group(left.pop())
print(len(find_group(0)), groups)