"""Single-parent synchronous copy networks after the unique input is clamped."""
from itertools import product
from collections import Counter
from roles10 import learn,observe
import json

def trace(parents,bit):
    s=[bit,0,0,0];values=[s[3]]
    for _ in range(6):
        s=[s[i] for i in parents];s[0]=0;values.append(s[3])
    return values
def run():
    population=Counter()
    for w in product(range(4),repeat=4):
        v=trace(w,1)
        # First backward hit of the source at a positive lag.
        node=3;visited=set();predicted=0
        while node and node not in visited:
            visited.add(node);node=w[node]
        if node==0:predicted=len(visited)
        assert [i for i in range(1,7) if v[i]]==([predicted] if predicted else [])
        population[predicted]+=1
    obs={}
    for key,w in [('fixed',(0,1,2,3)),('shift',(0,0,1,2)),('trained',learn(observe(.05)))]:
        obs[key]=[sum(trace(w,b)[t]==b for b in (0,1)) for t in (3,6)]
    # A latch with an extra OR operation can store the positive pulse at the fourth unit.
    for bit in (0,1):
        s=[bit,0,0,0]
        for t in range(1,7):
            s=[0,s[0],s[1],s[2]|s[3]]
            if t in (3,6):assert s[3]==bit
    return dict(wirings=256,first_hit={str(k):v for k,v in sorted(population.items())},correct_out_of_two=obs,with_or_latch=[2,2])
if __name__=='__main__':print(json.dumps(run(),indent=2))
