// grid.mjs — the free ladder against the anchored ladder over a wide (x, q) grid. // The free ladder is the longest run of consecutive T_x slots whose residues mod q // lie in a single translate of {0, q-2}; the anchored ladder is the a = 0 member. import { buildTile, gap6Of } from './job1493-tiles.mjs'; import { mergeSegments, laddersOf } from './job1493-merge.mjs'; import { writeFileSync } from 'node:fs'; const tiles = {}, Wof = {}; for (const x of [7, 11, 13, 17, 19, 23]) { const t = buildTile(x); tiles[x] = gap6Of(t.slots, t.W).g6; Wof[x] = t.W; } function ladders(g6, q) { const D = g6.length, qm = new Uint8Array(256); for (let b = 0; b < 256; b++) qm[b] = (6 * b) % q; const cur = new Int32Array(q), last = new Float64Array(q).fill(-2), best = new Int32Array(q); let r = 5 % q, n = 0, p0 = -1, p1 = -1, v0 = 0, v1 = 0; for (let i = 0; i < D; i++) { if (n === 0) { p0 = r; p1 = (r + 2) % q; v0 = 1; v1 = 1; } else { const r2 = (r + 2) % q; if (v0 === n && (p0 === r || p0 === r2)) v0 = n + 1; if (v1 === n && (p1 === r || p1 === r2)) v1 = n + 1; } let a = r; cur[a] = (last[a] === n - 1 ? cur[a] : 0) + 1; last[a] = n; if (cur[a] > best[a]) best[a] = cur[a]; a = (r + 2) % q; cur[a] = (last[a] === n - 1 ? cur[a] : 0) + 1; last[a] = n; if (cur[a] > best[a]) best[a] = cur[a]; n++; r += qm[g6[i]]; if (r >= q) r -= q; } const pref = [], suf = []; if (v0 > 0) pref.push([p0, v0]); if (v1 > 0) pref.push([p1, v1]); for (let a = 0; a < q; a++) if (last[a] === D - 1 && cur[a] > 0) suf.push([a, cur[a]]); const best2 = mergeSegments([{ n: D, pref, suf, best: Array.from(best) }], q, true); return { ...laddersOf(best2), D }; } const QS = [13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]; const out = {}; let cells = 0, freeBigger = 0, gapMax = 0, withGap = []; for (const x of Object.keys(tiles)) { out['T' + x] = {}; for (const q of QS) { if (q <= Number(x)) continue; const a = ladders(tiles[x], q); cells++; const d = a.L_free - a.L_anch; if (d > 0) { freeBigger++; withGap.push('T' + x + ' by ' + q + ':' + a.L_anch + '/' + a.L_free); } if (d > gapMax) gapMax = d; out['T' + x][String(q)] = { L_anch: a.L_anch, L_free: a.L_free, aStar: a.aStar, gap: d }; } } const res = { grid: out, cells, free_bigger_than_anchored: freeBigger, largest_gap: gapMax, cells_with_gap: withGap }; const GRID_OUT = process.env.GRID_OUT || 'grid.json'; writeFileSync(GRID_OUT, JSON.stringify(res, null, 1) + '\n'); console.log(JSON.stringify({ cells, free_bigger: freeBigger, largest_gap: gapMax, withGap: withGap.join(' ') }, null, 1));