1
0
Fork 0

Puzzle du 12 décembre 2017

Signed-off-by: Lucidiot <15960782+Lucidiot@users.noreply.github.com>
This commit is contained in:
Lucidiot 2017-12-12 07:32:07 +01:00
parent e59e8a584f
commit e1e74d49ea
No known key found for this signature in database
GPG Key ID: 2C2A322692E18B6B
1 changed files with 29 additions and 0 deletions

29
2017/12/pipes.py Normal file
View File

@ -0,0 +1,29 @@
#!/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)