"""Two derivations of one wheel frequency, with checked side conditions and costs."""
from fractions import Fraction as F
from itertools import combinations
from transport09 import counts

def run():
    src=(3,2,1,0)
    small=counts(src,True)[0];large=counts(src,False)[0]
    assert large==[8*n for n in small]
    rules=[('full',('case',),'full_table',40320),('read_full',('full_table',),'goal',1),
           ('pinned',('case',),'pinned_table',5040),('rotation',('case',),'equal_fibres',8),
           ('transfer',('pinned_table','equal_fibres'),'goal',1)]
    def optimum(unit):
        solutions=[]
        for n in range(len(rules)+1):
            for subset in combinations(rules,n):
                facts={'case'};cost=0;path=[]
                for name,needs,g,w in subset:
                    if set(needs)<=facts and g not in facts:
                        facts.add(g);cost+=1 if unit else w;path.append(name)
                if 'goal' in facts:solutions.append((cost,path))
        return min(solutions)
    facts={"case","pinned_table"}
    transfer=rules[-1]
    assert set(transfer[1])-facts=={"equal_fibres"}
    target=F(large[0],sum(large));other=F(2*small[0],sum(small)+small[0])
    assert other!=target and optimum(True)[0]==2 and optimum(False)[0]==5049
    return dict(value=str(target),weighted_counterexample=str(other),unit=optimum(True),visits_proxy=optimum(False),
                subsets=32,rules=rules,missing='equal_fibres')
if __name__=='__main__':
    import json
    print(json.dumps(run(),indent=2))
