#!/usr/bin/env node // close2.mjs - job #2747: exhaustive exact repair at depth K <= 2 (UTF-8). // For a state (residues of the first n primes), enumerate EVERY reassignment of one prime (K = 1) and of every pair of // distinct primes (K = 2) to any residue other than its current one, and score each candidate by the fewest holes in // any window [t, t+R) with 0 <= t <= L-R, L = R + pad (the producer's own buffer, pad = 200). Complete enumeration: // "no candidate reaches 0" is a proof for that K on this buffer. stdout = JSON artifact. import fs from 'node:fs'; const PR = [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]; const arg = (k, d) => { const i = process.argv.indexOf('--' + k); return i > 0 ? process.argv[i + 1] : d; }; const n = +arg('level'), R = +arg('R'), pad = +arg('pad', '200'), L = R + pad; const d = JSON.parse(fs.readFileSync(arg('residues'), 'utf8')); const res = (arg('key') ? d[arg('key')] : d.residues).slice(0, n).map(Number); const cls = (p) => (p === 2 ? [1] : [1, p - 1]); const cnt = new Int16Array(L); const add = (i, r, dlt) => { const p = PR[i]; for (const c of cls(p)) for (let s = ((c - r) % p + p) % p; s < L; s += p) cnt[s] += dlt; }; for (let i = 0; i < n; i++) add(i, res[i], 1); function minWindow() { // fewest zero-count offsets in any length-R window of [0, L) let h = 0; for (let s = 0; s < R; s++) if (cnt[s] === 0) h++; let best = h; for (let t = 1; t + R <= L; t++) { if (cnt[t - 1] === 0) h--; if (cnt[t + R - 1] === 0) h++; if (h < best) best = h; } return best; } const base = minWindow(); const out = { tool: 'close2.mjs/1 (job 2747)', level: n, R, L, residues: res, base_holes: base, K: {} }; const t0 = Date.now(); let k1 = { candidates: 0, min_holes: Infinity, solutions: 0, histogram: {} }; for (let i = 0; i < n; i++) for (let r = 0; r < PR[i]; r++) { if (r === res[i]) continue; add(i, res[i], -1); add(i, r, 1); const h = minWindow(); k1.candidates++; k1.min_holes = Math.min(k1.min_holes, h); if (h === 0) k1.solutions++; k1.histogram[h] = (k1.histogram[h] || 0) + 1; add(i, r, -1); add(i, res[i], 1); } out.K[1] = k1; let k2 = { candidates: 0, min_holes: Infinity, solutions: 0, first_solution: null, count_le_base: 0 }; for (let i = 0; i < n; i++) for (let ri = 0; ri < PR[i]; ri++) { if (ri === res[i]) continue; add(i, res[i], -1); add(i, ri, 1); for (let j = i + 1; j < n; j++) for (let rj = 0; rj < PR[j]; rj++) { if (rj === res[j]) continue; add(j, res[j], -1); add(j, rj, 1); const h = minWindow(); k2.candidates++; if (h < k2.min_holes) k2.min_holes = h; if (h <= base) k2.count_le_base++; if (h === 0) { k2.solutions++; if (!k2.first_solution) k2.first_solution = { i, ri, j, rj }; } add(j, rj, -1); add(j, res[j], 1); } add(i, ri, -1); add(i, res[i], 1); } out.K[2] = k2; out.proof_of_negative = k1.solutions === 0 && k2.solutions === 0; process.stderr.write(`close2 ${((Date.now() - t0) / 1000).toFixed(1)} s\n`); process.stdout.write(JSON.stringify(out, null, 1) + '\n');