# independent recount of crrr.py's table: different parsing (split cells, not a regex)
import json, sys
root = sys.argv[1]
codes = {}
for line in open(root + "/recompute/FINAL_ADJUDICATED_CODES.md"):
    if not line.startswith("|M"):
        continue
    cells = [c.strip().strip("*").strip() for c in line.strip().strip("|").split("|")]
    for i in range(0, len(cells) - 1, 2):
        codes[cells[i]] = cells[i + 1]
rr = {1, 5, 8, 9}
tab = {}
skipped = []
for line in open(root + "/recompute/sealed_key.jsonl"):
    d = json.loads(line)
    t = d["trial"]                      # trial_10_r1, or "baseline"
    if t == "baseline":
        skipped.append(d["item_id"]); continue
    tmpl = int(t.split("_")[1])
    cls = "RR" if tmpl in rr else "CR"
    cond = d["condition"][len("condition_"):].removesuffix("_fetch")
    arm = d["arm"].rsplit("_", 1)[1]
    k = (cond, arm, cls)
    hit, n = tab.get(k, (0, 0))
    tab[k] = (hit + (codes[d["item_id"]] in ("D", "SC")), n + 1)
print(len(codes), "codes; baseline rows outside the table:", skipped)
for k in sorted(tab):
    print(k, tab[k][0], "/", tab[k][1])
