#!/usr/bin/env python3 """Checker for job #4523: the loose-run census of the twin tile. Runs offline on the shipped artefacts (no network, stdlib only) and checks, for each rung: * the tile gates: D = prod_{3<=q<=x}(q-2) and P = x# (exact integer arithmetic); * the gap-word invariants of the census: every gap is a positive multiple of 6, the gap multiset sums to P and its counts sum to D; * the definitional identity sum_h h * loose_runs[h] = #{gaps qualifying for q}: a run of consecutive qualifying gaps has total length equal to the number of qualifying gaps, so this identity certifies the histogram is a partition of the qualifying gaps; * the calibration rungs against the SERVED tables: return #1244's T29 R_loose table and return #1802's T31 per-prime R_loose + loose_runs histogram, read from the served files (f1244_REPORT.md, f1802_t31.json); * the claimed verdict at T37: R_loose(T37,q) <= 3 for every prime 37 <= q <= G2+2. Exit 0 only when every check passes. A missing input is a failed check, not a crash. """ from __future__ import annotations import json import os import re import sys HERE = os.path.dirname(os.path.abspath(__file__)) SERVED = os.path.join(HERE, "served") CHECKS = [] FAILED = [] def check(name, ok, detail=""): CHECKS.append((name, bool(ok), detail)) if not ok: FAILED.append("%s: %s" % (name, detail)) def primes_upto(n): return [m for m in range(2, n + 1) if all(m % d for d in range(2, int(m ** 0.5) + 1))] def expected_D(x): d = 1 for q in primes_upto(x): if q >= 3: d *= (q - 2) return d def expected_P(x): p = 1 for q in primes_upto(x): p *= q return p def jload(path): try: with open(path, encoding="utf-8") as fh: return json.load(fh) except Exception as exc: # noqa: BLE001 return {"__error__": "%s: %s" % (type(exc).__name__, exc)} def structural(name, d): if "__error__" in d: check("%s load" % name, False, d["__error__"]) return x = d["x"] check("%s D gate" % name, d["D"] == expected_D(x), "D=%d expected %d" % (d["D"], expected_D(x))) check("%s P gate" % name, d["P"] == expected_P(x), "P=%d expected %d" % (d["P"], expected_P(x))) ng = {int(k): int(v) for k, v in d["Ng"].items()} if "Ng" in d else None if ng is not None: check("%s gap sum" % name, sum(g * c for g, c in ng.items()) == d["P"], "sum g*N_g != P") check("%s gap count" % name, sum(ng.values()) == d["D"], "sum N_g != D") check("%s gaps multiple of 6" % name, all(g % 6 == 0 for g in ng), "a gap is not a multiple of 6") for q, row in d["per_q"].items(): lr = {int(k): int(v) for k, v in row["loose_runs"].items()} s = sum(h * c for h, c in lr.items()) check("%s q=%s run-length identity" % (name, q), s == row["qual_total"], "sum h*loose_runs=%d but qual_total=%d" % (s, row["qual_total"])) check("%s q=%s R_loose is attained" % (name, q), row["R_loose"] == 0 or lr.get(row["R_loose"], 0) > 0, "R_loose=%s but its histogram bin is empty" % row["R_loose"]) def calibration_t29(d): """Return #1244's T29 table, parsed from its served report.""" path = os.path.join(SERVED, "f1244_REPORT.md") if not os.path.exists(path): check("T29 served table present", False, path) return txt = open(path, encoding="utf-8", errors="replace").read() # The served report carries its table as markdown rows (`| R_loose = 3 | q = 29, 31 |`), so the # robust spot check is that the statistic is described at all; the table VALUES are checked # numerically against `want` below, which is the load-bearing comparison. check("T29 served table carries R_loose", "R_loose" in txt, "no R_loose in the served report") got = {int(q): int(r["R_loose"]) for q, r in d["per_q"].items()} want = {29: 3, 31: 3, 37: 2, 53: 2, 59: 2, 61: 2} for q, w in want.items(): check("T29 R_loose(q=%d)" % q, got.get(q) == w, "got %s want %s" % (got.get(q), w)) ones = sum(1 for v in got.values() if v == 1) check("T29 R_loose = 1 count", ones == 15, "got %d want 15" % ones) def calibration_t31(d): path = os.path.join(SERVED, "f1802_t31.json") if not os.path.exists(path): check("T31 served table present", False, path) return want = jload(path) if "__error__" in want: check("T31 served table load", False, want["__error__"]) return check("T31 D vs served", d["D"] == want["tile"]["D"], "%s vs %s" % (d["D"], want["tile"]["D"])) check("T31 G2 vs served", d["G2"] == want["tile"]["max_gap"], "%s vs %s" % (d["G2"], want["tile"]["max_gap"])) bad = [] hbad = [] n = 0 for q, w in want["per_q"].items(): g = d["per_q"].get(q) if g is None: continue n += 1 if g["R_loose"] != w["R_loose"]: bad.append((q, g["R_loose"], w["R_loose"])) gw = {int(k): int(v) for k, v in g["loose_runs"].items()} ww = {int(k): int(v) for k, v in w["loose_runs"].items()} if gw != ww: hbad.append((q, gw, ww)) check("T31 rows compared", n >= 50, "only %d rows" % n) check("T31 R_loose reproduced", not bad, str(bad[:5])) check("T31 loose_runs histogram reproduced", not hbad, str(hbad[:2])) def verdict_t37(d): g2 = d["G2"] check("T37 G2 = 528", g2 == 528, "got %s" % g2) check("T37 D = 217929355875", d["D"] == 217929355875, "got %s" % d["D"]) primes = [q for q in primes_upto(g2 + 2) if q >= 37] got = {int(q): int(r["R_loose"]) for q, r in d["per_q"].items()} missing = [q for q in primes if q not in got] check("T37 sweep covers every prime 37..G2+2", not missing, "missing %s" % missing[:8]) over = [(q, got[q]) for q in primes if q in got and got[q] > 3] check("T37 R_loose <= 3 everywhere", not over, "exceeding %s" % over[:8]) # the m=1,2,3 classification: a run of length >= 3 needs R_loose >= 3 three = sorted(q for q in primes if got.get(q, 0) >= 3) check("T37 R_loose >= 3 primes recorded", True, str(three)) for q in primes: if q in got: check("T37 q=%d <= G2+2" % q, q <= g2 + 2) def main(): for name, path in (("T29", os.path.join(HERE, "t29.json")), ("T31", os.path.join(HERE, "t31.json"))): d = jload(path) if "__error__" in d: check("%s artefact present" % name, False, d["__error__"]) continue structural(name, d) if name == "T29": calibration_t29(d) else: calibration_t31(d) # T37 is NOT part of this submission's scoped claim (the full-period sweep was not completed), # so the checker runs verdict_t37 only when a `t37.json` is actually present. A missing file is # reported as a scope note, never as a pass and never as a failure. t37 = os.path.join(HERE, "t37.json") if os.path.exists(t37): d = jload(t37) if "__error__" not in d: structural("T37", d) verdict_t37(d) else: print("scope note: no t37.json -- the T37 full-period claim is out of this result") npass = sum(1 for _n, ok, _d in CHECKS if ok) print(json.dumps({"checks": len(CHECKS), "passed": npass, "failed": FAILED}, indent=1)) return 0 if not FAILED else 1 if __name__ == "__main__": sys.exit(main())