#!/usr/bin/env python3 """Independent stdlib checker for job 1412 (route 21, two-fold transport 43 -> 47 -> 53). Imports nothing from r21two.py / r21fast.py / #585. Reads results1412.json and, per cell: slots : x = 5 mod 6, gcd(x(x+2), 5*7*...*43) = 1, first N at or after the window start margin : scenario (c) one-step: 1 - w(K47(c)) - sum_{q=53..89} max_cc w(K_q(cc) - K47(c)) scenario (c,c') two-step: D = K47(c) u K53(c'); 1 - w(D) - sum_{q=59..103} max_cc w(K_q(cc) - D) K_t(c) = {x : x = c or c+2 mod t} lower : w >= 0, sum 1, min over all 47*53 two-step scenarios == claimed opt (exact Fractions) upper : y >= 0, sum 1; every certificate row a is a TRUE row (a = 1_D + sum_q 1_{K_q(s_q) - D} for some scenario and one class pair s_q per owner, found by search), and 1 - min_i sum_r y_r a_r[i] == opt (for every w in the simplex, min_s margin_s(w) <= sum_r y_r (1 - a_r.w) <= 1 - min_i (y.A)_i) Both bounds equal => opt is the exact joint two-step maximin value of that cell. """ import json, math, sys from fractions import Fraction as F PR = [p for p in range(2, 110) if all(p % d for d in range(2, int(p ** .5) + 1))] P43 = math.prod(p for p in PR if 5 <= p <= 43) Q47 = [q for q in PR if 47 < q <= 94] Q53 = [q for q in PR if 53 < q <= 106] def slots(ws, n): out, x = [], ws while x % 6 != 5: x += 1 while len(out) < n: if math.gcd(x * (x + 2), P43) == 1: out.append(x) x += 6 return out _KC = {} def K(xs, t, c): key = (xs[0], len(xs), t, c) if key not in _KC: _KC[key] = frozenset(i for i, x in enumerate(xs) if (x - c) % t in (0, 2)) return _KC[key] def margin(xs, w, dead, owners): """exact: w given as integers over a common denominator den (w = (ints, den)).""" wi, den = w m = den - sum(wi[i] for i in dead) for q in owners: m -= max(sum(wi[i] for i in K(xs, q, c) - dead) for c in range(q)) return F(m, den) def valid_row(xs, r, n): """the row must equal 1_D + sum_q 1_{K_q(s_q) - D} for its stated scenario and class pairs.""" sc, b = r["scenario"], r["binds"] if sc[0] == "2": D, owners = K(xs, 47, sc[1]) | K(xs, 53, sc[2]), Q53 else: D, owners = K(xs, 47, sc[1]), Q47 a = [1 if i in D else 0 for i in range(n)] for q in owners: if b[str(q)] is None: continue for i in K(xs, q, b[str(q)]) - D: a[i] += 1 return sc[0] == "2" and a == r["a"] def main(): R = json.load(open(sys.argv[1] if len(sys.argv) > 1 else "results1412.json")) allok = True for key, cell in sorted(R["cells"].items()): ws, n = cell["window_start"], cell["N"] xs = slots(ws, n) w = [F(v) for v in cell["w"]] assert len(w) == n and all(v >= 0 for v in w) and sum(w) == 1 opt = F(cell["opt"]) den = math.lcm(*[v.denominator for v in w]); wI = ([int(v * den) for v in w], den) two = [margin(xs, wI, K(xs, 47, c) | K(xs, 53, c2), Q53) for c in range(47) for c2 in range(53)] one = [margin(xs, wI, K(xs, 47, c), Q47) for c in range(47)] lower_ok = min(two) == opt rows = cell["cert_rows"] y = [F(r["y"]) for r in rows] up_ok = all(v >= 0 for v in y) and sum(y) == 1 cover = [sum(y[k] * rows[k]["a"][i] for k in range(len(rows))) for i in range(n)] up_ok &= (1 - min(cover)) == opt rows_ok = all(valid_row(xs, r, n) for r in rows) ok = lower_ok and up_ok and rows_ok and str(min(one)) == cell["one_step_min_at_w"] allok &= ok print(f"{key}: L={xs[-1]-xs[0]+1} opt={opt} ({float(opt):+.5f}) two-step pairs>0 {sum(v > 0 for v in two)}/2491 " f"one-step min {min(one)} lower={lower_ok} upper={up_ok} rows_valid={rows_ok} -> {'OK' if ok else 'FAIL'}", flush=True) print("ALL_OK" if allok else "SOME_FAIL") sys.exit(0 if allok else 1) if __name__ == "__main__": main()