// Numbers for the guides, from the site's real draw code (src/core/draw.ts). Nothing here is typed by hand: // the guide tables read src/seo/data/draw-stats.json, which this script writes. // // npx vite-node scripts/draw-stats.ts -- --write // // What it measures: // 1. exact: for groups of 3 to 10 people with 0 to n/2 couples (people 0-1, 2-3, ... marked "não se tiram"), // every permutation is enumerated over the draw's own rule matrix (allowedMatrix). Counted: valid draws, // valid draws that form one single chain (isSingleCycle), and whether the draw's feasibility check passes. // 2. households: for groups of 4 to 12, the largest "casa" (h people who may not draw each other) for which the // draw's feasibility check (checkFeasible) still passes. // 3. families: for bigger groups (12 to 30 people, 3 to 8 couples), seeded runs of draw() itself: // - without marking the couples, the share of draws where some partner draws the other; // - with the couples marked, how many shuffles the draw's first strategy needs on average (1 / acceptance); // - with "corrente única" on, the share of draws that come out as one single chain. // Seeds are fixed, so running it again gives the same file. import { writeFileSync, mkdirSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { allowedMatrix, checkFeasible, draw, isSingleCycle, isValid } from '../src/core/draw'; import { makeRng, shuffle } from '../src/core/random'; export const couplesOf = (k: number): [number, number][] => Array.from({ length: k }, (_, i) => [2 * i, 2 * i + 1]); export interface ExactRow { n: number; k: number; feasible: boolean; permutations: number; valid: number; singleChain: number } /** Every permutation of n people under the draw's rule matrix, by backtracking over allowed receivers. */ export function exact(n: number, k: number): ExactRow { const input = { n, pairs: couplesOf(k) }; const allowed = allowedMatrix(input); const to = new Array(n).fill(-1); const taken = new Array(n).fill(false); let valid = 0, singleChain = 0; const go = (g: number) => { if (g === n) { if (!isValid(to, allowed)) throw new Error('enumerated an invalid draw'); valid++; if (isSingleCycle(to)) singleChain++; return; } for (let r = 0; r < n; r++) { if (taken[r] || !allowed[g][r]) continue; to[g] = r; taken[r] = true; go(g + 1); taken[r] = false; to[g] = -1; } }; go(0); let permutations = 1; for (let i = 2; i <= n; i++) permutations *= i; return { n, k, feasible: n >= 3 && checkFeasible(input) === null, permutations, valid, singleChain }; } /** All pairs among the first h people: a household where nobody draws anybody else. */ export const householdPairs = (h: number): [number, number][] => { const p: [number, number][] = []; for (let a = 0; a < h; a++) for (let b = a + 1; b < h; b++) p.push([a, b]); return p; }; export function largestHousehold(n: number): number { let best = 1; for (let h = 2; h <= n; h++) if (checkFeasible({ n, pairs: householdPairs(h) }) === null) best = h; return best; } export interface FamilyRow { n: number; k: number; runs: number; feasible: boolean; couplesHitUnmarked: number; avgShuffles: number; singleChainWhenAsked: number; singleChainWhenNotAsked: number } export function family(n: number, k: number, runs: number): FamilyRow { const pairs = couplesOf(k); const partner = new Map(); for (const [a, b] of pairs) { partner.set(a, b); partner.set(b, a); } let hit = 0, chainAsked = 0, chainNot = 0; for (let i = 0; i < runs; i++) { const free = draw({ n, seed: `free-${n}-${k}-${i}` }); if (!free.ok) throw new Error('unrestricted draw failed'); if (free.to.some((r, g) => partner.get(g) === r)) hit++; const asked = draw({ n, pairs, singleCycle: true, seed: `chain-${n}-${k}-${i}` }); if (asked.ok && asked.singleCycle) chainAsked++; const plain = draw({ n, pairs, seed: `plain-${n}-${k}-${i}` }); if (plain.ok && plain.singleCycle) chainNot++; } // The draw's first strategy: shuffle everyone, keep the result if it respects every rule (src/core/draw.ts). const allowed = allowedMatrix({ n, pairs }); const rng = makeRng(`shuffles-${n}-${k}`); const perm = [...Array(n).keys()]; const shuffles = runs * 10; let accepted = 0; for (let i = 0; i < shuffles; i++) { shuffle(rng, perm); if (perm.every((r, g) => allowed[g][r])) accepted++; } return { n, k, runs, feasible: checkFeasible({ n, pairs }) === null, couplesHitUnmarked: hit / runs, avgShuffles: shuffles / accepted, singleChainWhenAsked: chainAsked / runs, singleChainWhenNotAsked: chainNot / runs, }; } export const FAMILY_SIZES: [number, number][] = [[12, 3], [16, 4], [20, 5], [24, 6], [30, 8]]; export const FAMILY_RUNS = 2000; export function compute() { const exactRows: ExactRow[] = []; for (let n = 3; n <= 10; n++) for (let k = 0; k <= Math.floor(n / 2); k++) exactRows.push(exact(n, k)); const households = Array.from({ length: 9 }, (_, i) => ({ n: i + 4, largest: largestHousehold(i + 4) })); const families = FAMILY_SIZES.map(([n, k]) => family(n, k, FAMILY_RUNS)); return { script: 'scripts/draw-stats.ts', drawCode: 'src/core/draw.ts', exact: exactRows, households, families }; } if (process.argv.includes('--write')) { const out = resolve(__dirname, '../src/seo/data/draw-stats.json'); mkdirSync(dirname(out), { recursive: true }); const t = Date.now(); const data = compute(); writeFileSync(out, JSON.stringify(data, null, 1) + '\n'); console.log(`wrote ${out} in ${((Date.now() - t) / 1000).toFixed(1)} s`); }