summaryrefslogtreecommitdiff
path: root/connected.py
blob: ee3ae70d8de7f84d488786783f51147a5645e5ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
from sys import argv
if len(argv) == 1: NUM = 1
if len(argv) == 2: NUM = int(argv[1])

def is_connected(capset0, capset1):
    count = 0
    for card in capset0:
        if card in capset1:
            continue
        count += 1
    return count <= NUM

def next_connected(disconnected):
    newly_connected = [disconnected.pop()]
    connected = []
    while newly_connected:
        check_capsets = iter(newly_connected[:])
        connected.extend(newly_connected)
        newly_connected = []
        for con_capset in check_capsets:
            for index, dis_capset in reversed(list(enumerate(disconnected[:]))):
                if is_connected(con_capset, dis_capset):
                    newly_connected.append(disconnected.pop(index))
    return connected

def main():
    capsets = []
    capset_file = open("capset.out", "r")
    for line in capset_file:
        capsets.append(tuple(line[1:-2].split(" ")))
    capset_file.close()

    connecteds = []
    while capsets:
        connecteds.append(next_connected(capsets))
    #for connected in connecteds:
    #    print("\nThe following are connected:")
    #    for capset in connected:
    #        print(capset)
    print("\nThere are", len(connecteds), "partitions.")
    lengths = list(map(len, connecteds))
    for length in sorted(set(lengths)):
        print("There are", lengths.count(length), "with length", length)

if __name__ == "__main__": main()