/machines/1RB1LC_1RC---_1LD1LC_1RE0LC_1RF0RE_1RA0RD/scripts/deepen2_chainval.py · raw
# Validate the danger chain on the isolated cluster "11 01 00" (cells 1 1 0 1 0 0) inside alternating runs.
# Each "signal" = a right-moving head entering from the far left in a chosen phase (E or x), run until it leaves left.
# Enumerate all phase sequences of length <=4, random run lengths m,n in 6..60; report outcome per sequence.
import random, itertools
T={('A',0):(1,1,'B'),('A',1):(1,-1,'C'),('B',0):(1,1,'C'),('B',1):None,('C',0):(1,-1,'D'),('C',1):(1,-1,'C'),
   ('D',0):(1,1,'E'),('D',1):(0,-1,'C'),('E',0):(1,1,'F'),('E',1):(0,1,'E'),('F',0):(1,1,'A'),('F',1):(0,1,'D')}
KAP={('E',0):0,('F',1):1,('D',0):2,('E',1):3}
STATE={0:('E',0),1:('F',1),2:('D',0),3:('E',1)}
def build(m,n,core):
    # left run of m alternating cells ending with the complement of core[0]; right run alternating after core[-1]
    left=[(1-core[0]) if (m-i)%2==1 else core[0] for i in range(m)]
    right=[(1-core[-1]) if i%2==0 else core[-1] for i in range(n)]
    return left+core+right
def defects(t):
    s=''.join(map(str,t)); return s
def send(t, phase, maxsteps=200000):
    # head enters at cell 0 moving right; choose state so that the arrival at the first defect is E or x.
    # find first defect position q (t[q]==t[q+1])
    q=next(i for i in range(len(t)-1) if t[i]==t[i+1])
    # clock at q must be: for t[q]==1: E-phase kappa 3, x-phase kappa 1; for t[q]==0: E 0, x 2
    want={(1,'E'):3,(1,'x'):1,(0,'E'):0,(0,'x'):2}[(t[q],phase)]
    k0=(want-q)%4
    st,v=STATE[k0]
    assert t[0]==v, (t[0],v,k0)
    t=list(t); pos=0; steps=0
    while 0<=pos<len(t) and steps<maxsteps:
        tr=T[(st,t[pos])]
        if tr is None: return 'HALT',t
        w,d,ns=tr; t[pos]=w; pos+=d; st=ns; steps+=1
    if pos>=len(t): return 'RIGHT',t
    if pos<0:
        # head leaves to the left; the next signal will re-enter from cell 0.
        return 'LEFT',t
    return 'TIMEOUT',t
random.seed(11)
core=[1,1,0,1,0,0]
res={}
for L in range(1,5):
    for seq in itertools.product('Ex',repeat=L):
        outs=set()
        for _ in range(40):
            m=random.choice(range(8,61,1)); n=random.randint(8,60)
            t=build(m,n,core); out=None
            for i,ph in enumerate(seq):
                r,t=send(t,ph)
                if r!='LEFT': out=(i,r); break
            if out is None:
                s=''.join(map(str,t)); i0=next(i for i in range(len(t)-1) if t[i]==t[i+1]); out=('ok', s[m-2:m+8])
            outs.add(out if out[0]!='ok' else ('ok',))
        res[''.join(seq)]=outs
for k,v in res.items(): print(k, sorted(v))
print('--- length 5/6 sequences starting xExx')
for seq in ['xExxE','xExxx','xExxxE','xExxxx']:
    outs=set()
    for _ in range(60):
        m=random.randint(8,60); n=random.randint(8,60); t=build(m,n,core); out=None
        for i,ph in enumerate(seq):
            r,t=send(t,ph)
            if r!='LEFT': out=(i,r); break
        outs.add(out if out else ('ok',''.join(map(str,t))[m-2:m+10]))
    print(seq, sorted(outs)[:4], len(outs))