"""Exact small-word checks for a single modulo-5 phase, plus custody preflight.""" import argparse import collections import json import math import resource import sys from fractions import Fraction from pathlib import Path STATES = (1, 2, 4) def profiles(total, length=5): if length == 1: yield (total,) else: for first in range(total + 1): for rest in profiles(total - first, length - 1): yield (first,) + rest def balance(m): return m[2] - m[3] - 2 * (m[1] - m[4]) - 1 def generating_function_count(m): if balance(m): return 0 _, m1, m2, m3, m4 = m result = 0 for seed in (0, 1): for a in range(min(m1, m4 - seed) + 1): c = m1 - a d = m4 - a - seed b = m2 - 2 * c - (1 - seed) if min(b, c, d) < 0 or b + 2 * d + seed != m3: continue numerator = math.factorial(a + b + c + d) * 2**b denominator = math.prod(math.factorial(v) for v in (a, b, c, d)) quotient, remainder = divmod(numerator, denominator) assert remainder == 0 result += quotient return result * math.comb(sum(m), m[0]) def flow_range(m): if balance(m): return range(0) difference = m[1] - m[4] return range(max(0, difference), min(m[2], m[3] + difference) + 1) def flow_counts(m): assert sum(m) <= 8, "Large-count flow sampling is not implemented" weights = [] a, b = m[1], m[4] for c in flow_range(m): d = m[2] - c f = c - a + b e = m[3] - f assert min(a, b, c, d, e, f) >= 0 out1, out2, out4 = a + e, b + c, d + f assert out4 > 0 if out2 == 0: # State2 is absent; the nonzero path alternates 4->1->4->...->1. assert a == b == c == f == 0 and d == e + 1 count = 1 else: tree = b * (d + f) + c * d numerator = tree * math.factorial(out1) * math.factorial(out2 - 1) * math.factorial(out4 - 1) denominator = math.prod(math.factorial(v) for v in (a, b, c, d, e, f)) count, remainder = divmod(numerator, denominator) assert remainder == 0 weights.append((c, count * math.comb(sum(m), m[0]))) return weights def flow_ratio(m, c): a, b = m[1], m[4] if a == b == 0: return Fraction(m[3] - c, c + 1) d = m[2] - c f = c - a + b e = m[3] - f tree = b * (d + f) + c * d next_tree = b * (d + f) + (c + 1) * (d - 1) return Fraction( next_tree * (b + c) * d * e, tree * (a + e) * (c + 1) * (f + 1), ) def enumerate_paths(length): counts = collections.Counter() def visit(state, remaining, residues): if remaining == 0: if state == 1: phase = 4 for residue in residues + [1] + residues[::-1]: phase = (phase + residue) % 5 assert phase in STATES assert phase == 4 counts[tuple(residues.count(r) for r in range(5))] += 1 return for successor in STATES: visit(successor, remaining - 1, residues + [(successor - state) % 5]) visit(4, length, []) return counts def main(): parser = argparse.ArgumentParser() parser.add_argument("input") parser.add_argument("target", nargs="?") args = parser.parse_args() data = json.loads(Path(args.input).read_text()) checked = words = 0 for length in range(9): literal = enumerate_paths(length) assert sum(literal.values()) == (3 ** (length - 1) if length else 0) words += sum(literal.values()) for m in profiles(length): count = generating_function_count(m) assert count == literal[m] flow = flow_counts(m) assert sum(weight for _, weight in flow) == count for (c, weight), (_, next_weight) in zip(flow, flow[1:]): assert flow_ratio(m, c) == Fraction(next_weight, weight) checked += 1 example = (0, 0, 3, 2, 0) weights = flow_counts(example) assert weights == [(0, 1), (1, 2), (2, 1)] custody = [] for level in data["levels"]: full = {int(k): v for k, v in level["full_gap_counts"].items()} assert all(k > 0 and k % 6 == 0 and v > 0 for k, v in full.items()) assert all(v % 2 == (k == 6) for k, v in full.items()) assert sum(full.values()) == level["slots"] assert sum(k * v for k, v in full.items()) == level["period"] half = {k: (v - (k == 6)) // 2 for k, v in full.items()} m = tuple(sum(v for k, v in half.items() if k % 5 == r) for r in range(5)) assert 2 * sum(k * v for k, v in half.items()) + 6 == level["period"] assert balance(m) == 0, "Published custody fails the common-phase balance identity" possibilities = flow_range(m) assert possibilities custody.append({ "x": level["x"], "half_length": sum(m), "residue_counts": list(m), "balance_residual": balance(m), "free_flow_min": possibilities.start, "free_flow_max": possibilities.stop - 1, "compatible_flow_types": len(possibilities), "large_word_count_or_sample_computed": False, }) output = { "scope": "New exact toy counts and copied-histogram phase-balance preflight; no wheel or production null draw", "marked_states": {"start": 4, "end": 1, "allowed": list(STATES)}, "nonzero_generating_function": "(z2+z3*z4)/(1-z1*z4-2*z2*z3-z1*z2^2-z4*z3^2)", "toy_max_length": 8, "all_count_profiles_checked": checked, "admissible_words_enumerated": words, "methods": ["literal state paths", "transfer-matrix coefficient formula", "open Euler-trail weights"], "nonuniform_flow_example": {"residue_counts": list(example), "flow_weights": weights}, "custody": custody, } # Normalize tuples before comparing the JSON target. output = json.loads(json.dumps(output)) if args.target: assert output == json.loads(Path(args.target).read_text()) print(json.dumps(output, indent=2)) if __name__ == "__main__": resource.setrlimit(resource.RLIMIT_AS, (512 * 1024**2,) * 2) resource.setrlimit(resource.RLIMIT_CPU, (30,) * 2) before = resource.getrusage(resource.RUSAGE_SELF) main() after = resource.getrusage(resource.RUSAGE_SELF) print(json.dumps({ "cpu_seconds": after.ru_utime + after.ru_stime - before.ru_utime - before.ru_stime, "maxrss_kib_linux": after.ru_maxrss, }), file=sys.stderr)