# -*- coding: utf-8 -*-
import json, pathlib, re, sys, io, collections
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = pathlib.Path(r"D:\work buddy\2026-11-05-14-15-52")
html = (BASE / "index_main.html").read_text(encoding="utf-8", errors="replace")
seg = html[html.index("const PROGRAMS=["):html.index("const CATD=[")]
disc = {}
for b in seg.split('{id:"')[1:]:
    pid = b[:b.index('"')]
    m = re.search(r'disc:"([^"]*)"', b)
    disc[pid] = m.group(1) if m else ""
rows = json.load(open(BASE / "_density_report.json", encoding="utf-8"))["rows"]
non = [r for r in rows if r["grade"] != "A"]
cnt = collections.Counter(disc.get(r["id"], "") for r in non)
print(cnt.most_common())
for d in ["cs", "eng", "fin", "econ", "biz", "sci", "stat"]:
    hit = [r for r in non if disc.get(r["id"], "") == d]
    print(f"== {d} {len(hit)}")
    for r in sorted(hit, key=lambda x: -x["score"])[:12]:
        print(f"  {r['grade']}{r['score']} {r['id']} | {r['n'][:38]} | {r['url'][:60]} | c{r['m']['cards']} ch{r['m']['chars']} cd{r['m']['codes']}")
