/machines/1RB0LA_1RC---_0LD1RD_0RA0RE_0RF1RC_1LF0LA/scripts/validate_increment.py · raw
"""Validate the record rule: each C right record (standard form) shows value V+1, where a pair
written '4 3' is digit 0 (cells 1^2 ... wait: R-form 1^4 0 1^3) with a pending carry into the pair on its left.
Random words with top digit 3 (all reachable counters have top digit 3), w pairs, odd V start."""
import random, sys
from counter_model import *
T = parse_tm(TM33)
def val(word):
    s = [int(c) for c in word]
    if len(s) % 2: return None
    v = 0; carry = 0
    pairs = [(s[j], s[j+1]) for j in range(0, len(s), 2)]
    n = len(pairs); tot = 0
    for k, p in enumerate(pairs):
        wgt = 4 ** (n - 1 - k)
        if p == (4, 3): tot += 0 * wgt + 4 * wgt  # 23 with pending carry
        elif p in D2V: tot += D2V[p] * wgt
        else: return None
    return tot
def follow(syms, nrec, trail=40, maxsteps=10**6):
    cells, head = build(syms, trail)
    pad = 20000
    tape = bytearray(pad) + bytearray(cells) + bytearray(pad)
    pos = head + pad; hi = pos; st = 2; out = []; i = 0
    while len(out) < nrec and i < maxsteps:
        tr = T[(st, tape[pos])]; i += 1
        if tr is None: out.append(('HALT', i)); break
        w, d, ns = tr; tape[pos] = w; pos += d; st = ns
        if pos > hi:
            hi = pos
            if st == 2:
                pr = parse_record(tape, pos)
                out.append((i, ''.join(map(str, pr[1])) if pr else None, pos - pad))
    return out
rng = random.Random(int(sys.argv[1]) if len(sys.argv) > 1 else 7)
ok = bad = 0; widths = (2, 3, 4, 5, 6, 8, 12, 23, 30, 40)
for w in widths:
    for _ in range(60):
        v = 3 * 4 ** (w - 1) + rng.randrange(4 ** (w - 1))
        if v % 2 == 0: v += 1
        if v >= 4 ** w - 40: continue
        syms = word_from_value(v, w)
        recs = follow(syms, 24)
        prev = v
        for r in recs:
            if r[0] == 'HALT': bad += 1; print('HALT', w, v); break
            vv = val(r[1]) if r[1] else None
            if vv == prev + 1: ok += 1; prev = vv
            else: bad += 1; print('mismatch', w, v, r, vv, prev); break
print('record transitions checked:', ok, 'failures:', bad)