"""Compare union counts with individual proof-path preimages on a finite wheel."""
from itertools import product
from collections import Counter
import json


def experiment():
    periods=(3,5,7)
    paths=((1,2),(2,4),(4,8))
    def survives(path,phase):
        return all(x%p!=a for x in path for p,a in zip(periods,phase))
    counts=[0]*len(paths)
    multiplicity=Counter()
    first=[0]*len(paths)
    for phase in product(*(range(p) for p in periods)):
        matches=[survives(path,phase) for path in paths]
        for i,b in enumerate(matches):counts[i]+=b
        multiplicity[sum(matches)]+=1
        if any(matches):first[matches.index(True)]+=1
    def intersection(mask):
        offsets={x for i,path in enumerate(paths) if mask>>i&1 for x in path}
        ways=1
        for p in periods: ways*=p-len({x%p for x in offsets})
        return ways
    union=sum((-1)**(mask.bit_count()+1)*intersection(mask) for mask in range(1,8))
    assert union==105-multiplicity[0] and counts==[intersection(1<<i) for i in range(3)]
    assert sum(first)==union
    return dict(periods=periods,paths=paths,individual=counts,union=union,naive=sum(counts),first_witness=first,
                multiplicity=dict(sorted(multiplicity.items())))


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