#!/usr/bin/env python3
"""Regenerate starter/bench: random 3-CNF at the satisfiability threshold.

Uniform random 3-CNF with round(4.26 n) clauses, like SATLIB's uf/uuf sets,
labelled by the reference DPLL of check_solver.py. File names do not reveal
the answer; the answers are in bench/answers.csv. Standard library only.
"""

import random
import sys
from pathlib import Path

from check_solver import random_3cnf, reference_solve, write_dimacs

SIZES = {50: 20, 75: 20, 100: 20, 125: 10, 150: 10}
SEED = 2026


def main():
    bench = Path(__file__).parent / "starter" / "bench"
    bench.mkdir(parents=True, exist_ok=True)
    for old in bench.glob("*.cnf"):
        old.unlink()
    rng = random.Random(SEED)
    rows = ["file,answer"]
    for n, count in SIZES.items():
        m = round(4.26 * n)
        for i in range(count):
            clauses = random_3cnf(n, m, rng)
            name = f"n{n:03d}_{i:02d}.cnf"
            write_dimacs(bench / name, n, clauses, comment=f"uniform random 3-CNF, n={n}, m={m}")
            answer = "SAT" if reference_solve(clauses) is not None else "UNSAT"
            rows.append(f"{name},{answer}")
            print(name, answer, flush=True)
    (bench / "answers.csv").write_text("\n".join(rows) + "\n")
    return 0


if __name__ == "__main__":
    sys.exit(main())
