#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Route 90 triage, second measurement: the true distance from the recorded state to the bar, and whether a bounded exact repair can close it. The record stores ONE residue tuple per level (the state AFTER all four trials), and its per-trial `holes` counts are not reproducible from that tuple under the A144311 +-1 definition the project's own verifier uses (the best contiguous run, 2027, DOES reproduce exactly, so the tuple is the right object). This script therefore measures the quantity the triage actually needs: * distance(R) = min over window starts of the number of holes in a window of length R; * and, for the best window at each R, an EXACT bounded repair search. Usage: python triage90-distance.py [--depth K] [--span N] """ import itertools import json import sys from bisect import bisect_left P25 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] def classes(p): return (1,) if p == 2 else (1, p - 1) def coverage(residues, span): cov = bytearray(span) for k, p in enumerate(P25): for c in classes(p): for i in range((c - residues[k]) % p, span, p): cov[i] = 1 return cov def hole_list(cov): return [i for i, v in enumerate(cov) if not v] def window_min(holes, R, span): best, start = None, None for s in range(0, span - R + 1): c = bisect_left(holes, s + R) - bisect_left(holes, s) if best is None or c < best: best, start = c, s if c == 0: break return best, start def shifted(residues, s): """Residues of the state translated so that offset i means the integer (s + i).""" return [(a + s) % p for a, p in zip(residues, P25)] def repair_window(win, R, depth): """Exact bounded repair of the holes of `win` in [0, R) by reassigning <= depth residues.""" if not win or not win.startswith("wr"): raise ValueError("only a translated residue tuple is accepted") return _repair(win_residues_of(win), R, depth) def win_residues_of(w): return w["residues"] def _repair(residues, R, depth): masks = [0] * R for k, p in enumerate(P25): for c in classes(p): for i in range((c - residues[k]) % p, R, p): masks[i] |= 1 << k holes = [i for i, m in enumerate(masks) if m == 0] if not holes: return residues[:], holes, {"note": "already covered at this R"} from collections import defaultdict by_mask = defaultdict(list) for i, m in enumerate(masks): by_mask[m].append(i) def cand(h): return [(k, (c - h) % p) for k, p in enumerate(P25) for c in classes(p)] def covers(k, r, i): return (r + i) % PRIMES[k] in classes(PRIMES[k]) tried = 0 for d in range(1, min(depth, len(holes)) + 1): for chosen in itertools.combinations(range(len(holes)), d): for assign in itertools.product(*[cand(holes[i]) for i in chosen]): if len({k for k, _ in assign}) != d: continue moves = dict(assign) if any(not any(covers(k, r, h) for k, r in moves.items()) for h in holes): continue smask = 0 for k in moves: smask |= 1 << k bad = 0 for m, offs in by_mask.items(): if m == 0 or (m & ~smask): continue for i in offs: if not any(covers(k, r, i) for k, r in moves.items()): bad += 1 break tried += 1 if bad == 0: nr = residues[:] for k, r in moves.items(): nr[k] = r return nr, holes, {"depth": d, "moves_tried": tried, "moved_primes": [PRIMES[k] for k in sorted(moves)], "new_residues": {str(PRIMES[k]): r for k, r in moves.items()}} return None, holes, {"depth_searched": depth, "moves_tried": tried} PRIMES = P25 def main(): target = sys.argv[1] depth = 2 if "--depth" in sys.argv: depth = int(sys.argv[sys.argv.index("--depth") + 1]) span = 4000 if "--span" in sys.argv: span = int(sys.argv[sys.argv.index("--span") + 1]) d = json.load(open(target, encoding="utf-8")) a = [int(v) for v in d["levels"]["n25"]["residues"]] cov = coverage(a, span) holes = hole_list(cov) print("best contiguous run:", max( (len(list(g)) for g in __import__("itertools").groupby(cov) if g[0]), default=0)) print("holes in [0,%d): %d, first %s" % (span, len(holes), holes[:12])) out = {"distance": [], "repairs": []} targets = [2028, 2050, 2100, 2204, 2250, 2300, 2400, 2454, 2500] for R in targets: m, s = window_min(holes, R, span) row = {"R": R, "min_holes": m, "start": s} out["distance"].append(row) print("R=%-5d min_holes=%-3d at start %-5d" % (R, m, s)) if m and m <= max(depth, 1): winres = shifted(a, s) nr, hs, info = _repair(winres, R, depth) ok = nr is not None row["repair"] = {"attempted_holes": len(hs), "found": ok, "info": info} if ok: row["repaired_residues"] = nr # verify the repaired state on the full line cov2 = coverage(nr, span) hl2 = hole_list(cov2) m2, s2 = window_min(hl2, R, span) row["repaired_min_holes"] = m2 print(" REPAIR FOUND depth=%s primes=%s -> min_holes now %s" % (info["depth"], info["moved_primes"], m2)) else: print(" no repair at depth %d (%d moves tried)" % (depth, info["moves_tried"])) with open(target.replace(".out.json", "") + ".distance90.json", "w", encoding="utf-8") as fh: json.dump(out, fh, indent=1) print("wrote", target.replace(".out.json", "") + ".distance90.json") if __name__ == "__main__": main()