import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; // Input coordinates: Daniel Munro's 45-cap witness in Leaps in Bounds. // https://github.com/daniel-munro/leaps-in-bounds/tree/main/certificates/cap-set // The four-row target table is from IsItSolved report iis:2611.00053. function ternarySpace(dimension) { const size = 3 ** dimension; const points = Array.from({ length: size }, (_, i) => i); const digits = points.map((point) => Array.from({ length: dimension }, (_, i) => Math.floor(point / 3 ** i) % 3)); const encode = (coordinates) => coordinates.reduce((value, digit, i) => value + digit * 3 ** i, 0); const plus = digits.map((left) => Uint16Array.from(digits, (right) => encode(left.map((a, i) => (a + right[i]) % 3)))); const negative = points.map((p) => plus[p][p]); const dot = digits.map((left) => Uint8Array.from(digits, (right) => left.reduce((sum, a, i) => sum + a * right[i], 0) % 3)); const normals = points.slice(1).filter((p) => digits[p].find((digit) => digit !== 0) === 1); return { size, points, digits, encode, plus, negative, dot, normals }; } function isCap(points, space) { const included = new Set(points); if (included.size !== points.length) return false; for (let i = 0; i < points.length; i++) { for (let j = i + 1; j < points.length; j++) { const third = space.negative[space.plus[points[i]][points[j]]]; if (included.has(third)) return false; } } return true; } function forSubsets(values, size, visit, prefix = [], start = 0) { if (size === 0) return Boolean(visit(prefix)); for (let i = start; i <= values.length - size; i++) { prefix.push(values[i]); const done = forSubsets(values, size - 1, visit, prefix, i + 1); prefix.pop(); if (done) return true; } return false; } function fibers(points, space, normal) { const sections = [[], [], []]; for (const point of points) sections[space.dot[normal][point]].push(point); return sections; } function isCube(points, space) { const included = new Set(points); for (const corner of points) { const other = points.filter((p) => p !== corner); if (forSubsets(other, 3, ([a, b, c]) => { const u = space.plus[a][space.negative[corner]]; const v = space.plus[b][space.negative[corner]]; const w = space.plus[c][space.negative[corner]]; const p = space.plus[a][v]; const q = space.plus[a][w]; const r = space.plus[b][w]; const s = space.plus[p][w]; const vertices = [corner, a, b, c, p, q, r, s]; return new Set(vertices).size === 8 && vertices.every((x) => included.has(x)); })) return true; } return false; } function hasParallelCubes(corner, space) { return space.normals.some((normal) => { const sections = fibers(corner, space, normal); const eight = sections.filter((section) => section.length === 8); return eight.length === 2 && eight.every((section) => isCube(section, space)); }); } function chooseTemplate(input, space5, space4) { const candidates = []; for (const normal of space5.normals) { const sections = fibers(input, space5, normal); const small = sections.findIndex((section) => section.length === 9); if (small < 0 || sections.filter((section) => section.length === 18).length !== 2) continue; const pivot = space5.digits[normal].findIndex((digit) => digit !== 0); const coordinate = (p) => space4.encode(space5.digits[p].filter((_, i) => i !== pivot)); const normalized = [0, 1, 2].map((j) => sections[(j + small) % 3].map(coordinate).sort((a, b) => a - b)); if (normalized.slice(1).every((corner) => hasParallelCubes(corner, space4))) { candidates.push({ normal, normalized }); } } // Every A2 direction qualifies, and Thackeray's Lemma 2.2 gives exactly 45. assert.equal(candidates.length, 45); const { normal, normalized } = candidates[0]; const template = normalized.flatMap((section, j) => section.map((y) => j * space4.size + y)); assert(isCap(template, space5)); console.log('A2 directions', candidates.length, 'chosen normal', space5.digits[normal].join('')); return { template, corners: normalized }; } function cornerMaps(from, to, space) { const anchor = from[0]; const directions = from.map((p) => space.plus[p][space.negative[anchor]]); const basisSpan = [0]; for (const v of directions) { if (basisSpan.includes(v)) continue; const oldSpan = basisSpan.slice(); basisSpan.push(...oldSpan.map((p) => space.plus[p][v])); basisSpan.push(...oldSpan.map((p) => space.plus[p][space.negative[v]])); } assert.equal(basisSpan.length, space.size); const coordinate = []; basisSpan.forEach((point, coefficient) => { coordinate[point] = coefficient; }); const checks = []; for (let size = 1; size < space.size; size *= 3) { checks[size] = directions.map((p) => coordinate[p]).filter((c) => size <= c && c < 3 * size); } const target = new Set(to); const image = new Uint16Array(space.size); const distinctMaps = new Map(); for (const origin of to) { image[0] = origin; const extend = (spanSize) => { if (spanSize === space.size) { const map = Uint8Array.from(coordinate, (c) => space.plus[image[c]][space.negative[origin]]); distinctMaps.set(Buffer.from(map).toString('base64'), map); return; } for (const p of to) { if (image.subarray(0, spanSize).includes(p)) continue; const v = space.plus[p][space.negative[origin]]; for (let i = 0; i < spanSize; i++) { image[spanSize + i] = space.plus[image[i]][v]; image[2 * spanSize + i] = space.plus[image[i]][space.negative[v]]; } if (checks[spanSize].every((c) => target.has(image[c]))) extend(3 * spanSize); } }; extend(1); } return [...distinctMaps.values()]; } function relativeMaps(corners, space) { const maps = [null, [null], [null]]; for (const i of [1, 2]) { for (const j of [1, 2]) maps[i][j] = cornerMaps(corners[i], corners[j], space); } const distinctMaps = new Map(); for (const left of [1, 2]) { for (const right of [1, 2]) { for (const transverse of [1, 2]) { for (const first of maps[transverse][left]) { for (const second of maps[right][3 - transverse]) { const composite = Uint8Array.from(second, (p) => first[p]); distinctMaps.set(Buffer.from(composite).toString('base64'), composite); } } } } } console.log('individual corner maps', maps[1].slice(1).map((m) => m.length), maps[2].slice(1).map((m) => m.length), 'composed maps', distinctMaps.size); return [...distinctMaps.values()]; } function checkCornerRigidity(corner, space) { const outside = space.points.filter((point) => !corner.includes(point)); const counts = []; for (const replaced of [1, 2, 3]) { let caps = 0; forSubsets(corner, replaced, (removed) => { const keep = corner.filter((point) => !removed.includes(point)); const blocked = new Set(); forSubsets(keep, 2, ([a, b]) => { blocked.add(space.negative[space.plus[a][b]]); }); const available = outside.filter((point) => !blocked.has(point)); forSubsets(available, replaced, (inserted) => { const updated = [...keep, ...inserted]; if (!isCap(updated, space)) return; caps++; assert(space.normals.some((normal) => fibers(updated, space, normal).some((section) => section.length === 9))); }); }); counts.push(caps); } return counts; } function signedMomentCertificate() { const choose2 = (n) => n * (n - 1) / 2; const choose3 = (n) => n * (n - 1) * (n - 2) / 6; const globalWeight = 4571 * 121 * choose2(109) - 137 * 40 * choose3(109) - 364 * 5782896; const positive = []; let remaining = 0; for (let middle = 0; middle <= 45; middle++) { for (let high = middle; high <= 45; high++) { const low = 109 - middle - high; if (low < 0 || low > middle || middle + high >= 88 || (middle === 42 && high === 45)) continue; remaining++; const weight = [high, middle, low] .reduce((total, n) => total + 4571 * choose2(n) - 137 * choose3(n), -5782896); if (weight > 0) positive.push([high, middle, low, weight]); } } console.log('signed moment', globalWeight, 'profiles', remaining, 'positive', JSON.stringify(positive)); assert.equal(globalWeight, 81162); assert.equal(remaining, 65); return positive.map(([high, middle, low]) => ({ left: 45 - high, right: 45 - middle, need: low })); } function canOpenEnough(smallSums, count, pairs, size, indices, profile) { let base = 0; const extra = new Uint8Array(size * size); for (const sum of smallSums) { const deficit = count[sum] - profile.right; if (deficit <= 0) { base++; continue; } const endpoints = Array.from({ length: count[sum] }, (_, i) => Math.floor(pairs[sum * size + i] / size)); if (profile.left === 1) { for (const i of endpoints) extra[i]++; } else if (deficit === 1) { for (const i of endpoints) { for (const j of indices) { if (i === j || (j < i && endpoints.includes(j))) continue; extra[Math.min(i, j) * size + Math.max(i, j)]++; } } } else { forSubsets(endpoints, 2, ([i, j]) => { extra[i * size + j]++; }); } } const leftDeleted = new Uint8Array(size); const rightDeleted = new Uint8Array(size); const weight = new Uint16Array(size); return forSubsets(indices, profile.left, (choice) => { const index = choice.length === 1 ? choice[0] : choice[0] * size + choice[1]; // A sum must have at most R live representations after the left deletions. if (base + extra[index] < profile.need) return false; for (const index of choice) leftDeleted[index] = 1; const masks = []; let open = 0; for (const sum of smallSums) { const required = []; for (let i = 0; i < count[sum]; i++) { const pair = pairs[sum * size + i]; if (!leftDeleted[Math.floor(pair / size)]) { required.push(pair % size); if (required.length > profile.right) break; } } if (required.length === 0) open++; else if (required.length <= profile.right) masks.push(required); } for (const index of choice) leftDeleted[index] = 0; if (open >= profile.need) return true; if (open + masks.length < profile.need) return false; // A fully hit mask contributes six units across its selected endpoints. weight.fill(0); for (const mask of masks) { for (const endpoint of mask) weight[endpoint] += 6 / mask.length; } let budget = 0; for (let n = 0; n < profile.right; n++) { let best = 0; for (const i of indices) if (weight[i] > weight[best]) best = i; budget += weight[best]; weight[best] = 0; } if (6 * open + budget < 6 * profile.need) return false; // Deleting any other endpoint has no effect on the candidate sums. const relevant = [...new Set(masks.flat())]; return forSubsets(relevant, Math.min(profile.right, relevant.length), (deletions) => { for (const index of deletions) rightDeleted[index] = 1; let holes = open; for (const mask of masks) { if (mask.every((index) => rightDeleted[index])) holes++; } for (const index of deletions) rightDeleted[index] = 0; return holes >= profile.need; }); }); } function checkPlacements(template, maps, space4, space5, profiles) { const size = template.length; const indices = template.map((_, i) => i); const count = new Uint8Array(space5.size); const pairs = new Uint16Array(space5.size * size); const rows = profiles.map((profile) => ({ ...profile, screen: 0, intact: 0, other: 0 })); let placements = 0; for (const map of maps) { for (const sign of [1, 2]) { for (const shift of space4.points) { placements++; const right = template.map((point) => { const j = Math.floor(point / space4.size); const offset = j === 0 ? 0 : j === 1 ? shift : space4.negative[shift]; return (sign * j % 3) * space4.size + space4.plus[map[point % space4.size]][offset]; }); count.fill(0); for (const i of indices) { const translate = space5.plus[template[i]]; for (const j of indices) { const sum = translate[right[j]]; pairs[sum * size + count[sum]++] = i * size + j; } } for (const row of rows) { const smallSums = space5.points.filter((p) => count[p] <= row.left + row.right); if (smallSums.length < row.need) continue; row.screen++; if (smallSums.length === 22 && smallSums.every((p) => count[p] === 0) && isCap(smallSums, space5)) { row.intact++; } else if (canOpenEnough(smallSums, count, pairs, size, indices, row)) { row.other++; console.log('surviving placement', row.left, row.right, sign, shift, Array.from(map)); } } } } } return { placements, rows }; } const space4 = ternarySpace(4); const space5 = ternarySpace(5); const input = readFileSync(new URL('./cap45.txt', import.meta.url), 'utf8').trim().split(/\s+/); assert(input.every((point) => /^[012]{5}$/.test(point))); const points = input.map((text) => space5.encode(Array.from(text, Number))); assert.equal(points.length, 45); assert(isCap(points, space5)); const { template, corners } = chooseTemplate(points, space5, space4); console.log('corner replacements with a 9-section', corners.slice(1).map((corner) => checkCornerRigidity(corner, space4))); const profiles = signedMomentCertificate(); const maps = relativeMaps(corners, space4); assert.equal(maps.length, 64); const result = checkPlacements(template, maps, space4, space5, profiles); console.log(JSON.stringify(result, null, 2)); assert.equal(result.placements, 10368); assert.deepEqual(result.rows, [ { left: 2, right: 3, need: 24, screen: 10160, intact: 0, other: 0 }, { left: 1, right: 3, need: 23, screen: 1584, intact: 0, other: 0 }, { left: 2, right: 2, need: 23, screen: 1584, intact: 0, other: 0 }, { left: 1, right: 2, need: 22, screen: 96, intact: 16, other: 0 }, ]);