"""audit-xor-update.py -- does the bitmask engine's incremental coverage update agree with a from-scratch recomputation? WHY. `repair90b.py` (route 90's negative results, return #1121 and #1105) maintains a single OR-mask U over the primes and moves one prime at a time with Up = (U ^ mask(k, cur_residue_k)) | mask(k, new_residue_k) XOR removes prime k's contribution only if no OTHER prime also sets those bits. Masks of a FIXED prime at different residues are indeed disjoint (that is what the engine's docstring justifies), but masks of DIFFERENT primes overlap freely: offset i is covered by prime p iff i = +-1 - v (mod p), which many primes satisfy at once. If the XOR flips a bit that another prime still covers, the update UNDERSTATES coverage, i.e. it invents holes. Invented holes are exactly what a depth-limited DFS would use to report "exhausted: no reassignment covers the window", so the direction of the possible error is the direction of every negative this route has filed. WHAT THIS SCRIPT DOES. On the served n = 25 state (the file route 90 declares as its input): 1. builds U0 by OR (both ways, to fix provenance), 2. for every prime k and every residue v != res[k], compares incremental (U0 ^ mask(k,res[k])) | mask(k,v) with the reference recomputation OR_j mask(j, u_j), u_j = res[j] except u_k = v, 3. reports how often they differ, by how many offsets, and in which direction. A reference recomputation is cheap (n primes, one mask each) so it can also be used as a correct update inside the search; this script measures whether that is necessary. """ import importlib.util import json import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) SRC = os.path.join(HERE, "repair90b.py") def load_engine(): spec = importlib.util.spec_from_file_location("repair90b", SRC) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def reference_U(bm, u): """OR of the mask of every prime at the residue given in u -- no incremental arithmetic.""" U = 0 for k in range(bm.n): U |= bm.mask(k, u[k]) return U def main(): R = load_engine() state = json.load(open(os.path.join(HERE, "state-n25.json"), encoding="utf-8")) res = state["residues"] n = len(res) print("state: n=%d served residues=%s" % (n, res)) for (start, Rlen) in [(227, 2069), (17, 2489)]: bm = R.BM(res, n, start, Rlen) U0 = bm.U0 ref0 = reference_U(bm, res) print("\n=== window [%d,%d) R=%d ===" % (start, start + Rlen, Rlen)) print("U0 == from-scratch OR: %s ; holes = %d" % (U0 == ref0, (bm.win & ~U0).bit_count())) bad = [] for k in range(n): p = R.PRIMES[k] for v in range(p): if v == res[k]: continue inc = (U0 ^ bm.mask(k, res[k])) | bm.mask(k, v) u = list(res) u[k] = v ref = reference_U(bm, u) if inc != ref: h_inc = (bm.win & ~inc).bit_count() h_ref = (bm.win & ~ref).bit_count() bad.append((k, p, v, h_inc, h_ref)) print("moves with an inconsistent update: %d / %d" % (len(bad), sum(R.PRIMES[k] - 1 for k in range(n)))) if bad: over = sum(1 for b in bad if b[3] > b[4]) print(" of those, invented-hole (h_inc > h_ref): %d" % over) print(" worst by invented holes:", sorted(bad, key=lambda b: b[4] - b[3])[:6]) print(" first few:", bad[:6]) return 0 if __name__ == "__main__": sys.exit(main())