#!/usr/bin/env node 'use strict'; // job #41: compare maxgap-law.js runs with its embedded OUTPUT block (the --big invocation). // Masks only what legitimately varies between runs: elapsed-time stamps ("12.3s", "(5.4s)", "done in 22.7s") // and the 30-second progress lines (" ... x=29 mode=1 34% 34.8s"), whose count depends on machine speed. // Usage: node compare-mg.js research/maxgap-law.js out-mg-big.txt [out-mg.txt] const fs = require('fs'); const { createHash } = require('node:crypto'); // Commit to every normalized line, including differences omitted from the display. const digest = lines => createHash('sha256').update(JSON.stringify(lines), 'utf8').digest('hex'); const src = fs.readFileSync(process.argv[2], 'utf8').split('\n'); const b = src.findIndex(l => l.startsWith('// body-lines:')); const n = Number(src[b].split(':')[1]); const h = src.findIndex((l, i) => i > b && l.startsWith('// =====')); const emb = src.slice(h + 1, h + 1 + n).map(l => l.replace(/^\/\/ ?/, '')); const mask = s => s.replace(/\b\d+\.\ds\b/g, '[T]'); const prog = s => /^\s+\.\.\. x=\d+ mode=\d+ \d+%/.test(s); const norm = arr => arr.filter(l => !prog(l)).map(mask); function compare(label, runFile, embLines) { const run = fs.readFileSync(runFile, 'utf8').replace(/\n$/, '').split('\n'); const E = norm(embLines), R = norm(run); let first = null, diffs = 0; const m = Math.max(E.length, R.length); for (let i = 0; i < m; i++) if (E[i] !== R[i]) { diffs++; if (first === null) first = i; } console.error(`${label}: raw embedded ${embLines.length} lines, raw run ${run.length} lines`); console.log(`${label}: embedded ${E.length} normalised lines, run ${R.length}; differing normalised lines ${diffs}`); if (first !== null) { for (let i = first; i < Math.min(first + 6, m); i++) console.log(` line ${i + 1}\n EMB: ${E[i]}\n RUN: ${R[i]}`); } console.log(`${label}: normalized SHA256 embedded ${digest(E)} run ${digest(R)}`); return { E, R }; } console.log(`embedded block: ${n} body lines`); compare('--big run vs embedded', process.argv[3], emb); if (process.argv[4]) { // the default run omits the x = 29 tile rows (and their progress lines); drop those rows from the embedded block const embNo29 = emb.filter(l => !/^\s+29\s+[12]\s+6469693230\b/.test(l)); const { E, R } = compare('default run vs embedded minus the two x=29 tile rows', process.argv[4], embNo29); // list the normalised lines that differ, grouped, to show which later-section lines depend on --big let shown = 0; for (let i = 0; i < Math.max(E.length, R.length) && shown < 40; i++) if (E[i] !== R[i]) { console.log(` L${i + 1} EMB: ${E[i]}\n RUN: ${R[i]}`); shown++; } }