// posthoc-hiding.mjs: EXPLORATORY, written AFTER measure.mjs's results were seen (not pre-registered).
// Question: why is the one-grid-step (k = 0) horizon so irregular? Hypothesis: a nudge that small
// can hide, because a disk's force is rounded to a whole number of grid steps before it acts, and a
// one-step change in position often rounds to the same forces. Measure, for each nudge size, how many
// steps pass before the disturbance reaches ANY other disk (another disk's integer position differs).
import { initial, IntGas } from "./engine.mjs";
import { writeFileSync } from "node:fs";
const out = {};
for (const k of [0, 1, 2, 4, 8]) {
  const waits = [];
  for (const seed of [1, 2, 3, 4, 5]) {
    const g = new IntGas(initial({ seed }));
    for (let start = 0; start < 8; start++) {
      g.step(250);
      for (const d of [0, 17, 42, 99]) {
        const p = g.clone(), q = g.clone(); q.nudge(d, 2 ** k);
        let s = 0, spread = false;
        while (s < 3000 && !spread) {
          p.step(1); q.step(1); s++;
          for (let i = 0; i < p.N; i++) if (i !== d && (p.X[i] !== q.X[i] || p.Y[i] !== q.Y[i])) { spread = true; break; }
        }
        waits.push(spread ? s : null);
      }
    }
  }
  const ok = waits.filter(v => v != null).sort((a, b) => a - b);
  out[k] = { n: waits.length, neverWithin3000: waits.length - ok.length, median: ok[ok.length >> 1], p90: ok[Math.floor(ok.length * 0.9)], waits };
  console.log(k, out[k].n, out[k].neverWithin3000, out[k].median, out[k].p90);
}
writeFileSync("posthoc-hiding.json", JSON.stringify(out) + "\n");
