Puzzle du 12 décembre 2017
Signed-off-by: Lucidiot <15960782+Lucidiot@users.noreply.github.com>
This commit is contained in:
parent
e59e8a584f
commit
e1e74d49ea
|
@ -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)
|
Loading…
Reference in New Issue