"""Cross-check type cardinalities by words and a recurrence over remaining edges."""
from collections import Counter
from functools import lru_cache
from itertools import product
import json
from math import comb


def experiment(n=12):
    groups=Counter()
    for tail in product(range(2),repeat=n-1):
        w=(0,)+tail
        c=Counter(2*x+y for x,y in zip(w,w[1:]))
        groups[tuple(c[i] for i in range(4))]+=1
    @lru_cache(None)
    def ways(x,counts):
        if sum(counts)==0:return 1
        total=0
        for y in range(2):
            i=2*x+y
            if counts[i]:
                after=list(counts);after[i]-=1
                total+=ways(y,tuple(after))
        return total
    assert all(ways(0,c)==v for c,v in groups.items())
    # Fixed known n and start; fixed-width index of a weak composition, then type rank.
    header=(comb(n+2,3)-1).bit_length()
    lengths=Counter()
    for c,v in groups.items():lengths[header+(v-1).bit_length()]+=v
    feasible_header=(len(groups)-1).bit_length()
    short_feasible=sum(v for c,v in groups.items() if feasible_header+(v-1).bit_length()<n-1)
    return dict(length=n,start=0,words=sum(groups.values()),types=len(groups),header_bits=header,
                raw_bits=n-1,coded_lengths=dict(sorted(lengths.items())),
                strictly_shorter=sum(v for k,v in lengths.items() if k<n-1),
                feasible_header_bits=feasible_header,shorter_with_shared_type_list=short_feasible)


if __name__=='__main__':print(json.dumps(experiment(),indent=2))
