"""combine_scores.py — compare Path 1 (PLN) vs Path 2 (BigG) on the 100 survivors."""
import subprocess, re

# Get PLN scores from candidates file
pln = {}
with open('mersenne_sieve_candidates.txt') as f:
    for line in f:
        m = re.search(r'p = (\d+).*score=([\d.]+)', line)
        if m:
            pln[m.group(1)] = float(m.group(2))

# Get BigG scores
result = subprocess.run(['.\\bigg_overfit.exe', '--score-file', 'candidates_plain.txt'],
                        capture_output=True, text=True)
bigg = {}
for line in result.stdout.splitlines():
    m = re.match(r'\s+(\d+)\s+yes\s+\S+\s+\S+\s+\S+\s+([\d.]+)', line)
    if m:
        bigg[m.group(1)] = float(m.group(2))

# Load candidates
with open('candidates_plain.txt') as f:
    candidates = [l.strip() for l in f if l.strip()]

# Normalize PLN to [0,1]
pln_max = max(pln.values()) if pln else 1.0
pln_min = min(pln.values()) if pln else 0.0
pln_range = max(pln_max - pln_min, 1e-9)

rows = []
for p in candidates:
    ps = pln.get(p, 0.0)
    bs = bigg.get(p, 0.0)
    pln_norm = (ps - pln_min) / pln_range if ps else 0.0
    consensus = (pln_norm + bs) / 2.0
    rows.append((p, ps, bs, pln_norm, consensus))

# Print sorted by consensus
rows_c = sorted(rows, key=lambda x: -x[4])
print("=== TOP 25 BY CONSENSUS (normalized PLN + BigG) ===")
print(f"{'p':>12}  {'PLN':>8}  {'PLN%':>6}  {'BigG':>6}  {'consensus':>9}")
print("-" * 52)
for p, ps, bs, pn, cs in rows_c[:25]:
    print(f"{p:>12}  {ps:8.5f}  {pn:6.2%}  {bs:6.4f}  {cs:9.4f}")

# Print sorted by BigG only
print()
print("=== TOP 20 BY BigG SCORE (Path 2 only) ===")
rows_b = sorted(rows, key=lambda x: -x[2])
print(f"{'p':>12}  {'BigG':>6}  {'PLN%':>6}")
print("-" * 30)
for p, ps, bs, pn, cs in rows_b[:20]:
    print(f"{p:>12}  {bs:6.4f}  {pn:6.2%}")

# Print where the two methods DISAGREE most
print()
print("=== LARGEST PLN vs BigG DISAGREEMENTS ===")
disagreements = sorted(rows, key=lambda x: abs(x[3] - x[2]), reverse=True)
print(f"{'p':>12}  {'PLN%':>6}  {'BigG':>6}  {'|diff|':>7}")
print("-" * 36)
for p, ps, bs, pn, cs in disagreements[:15]:
    print(f"{p:>12}  {pn:6.2%}  {bs:6.4f}  {abs(pn-bs):7.4f}")
