"""Summary of the table for two punctures at the budgets 30 to 33 (IIS-Q-0003).

usage: python3 -I work/capset/q0003/q3_p2_low_summary.py LOGDIR
The script reads the files LOGDIR/q3_p2_low_L0.txt and LOGDIR/q3_p2_low_L1.txt.  Each line has the
form "p2 a c | b: counts ... : ...".  For each layer and each budget it prints the largest number of
directions and the number of different rows.  It then compares the two layers row by row.
"""
import re
import sys
from collections import Counter
from pathlib import Path

logdir = Path(sys.argv[1])


def load(layer):
    table = {}
    for line in (logdir / ("q3_p2_low_L%d.txt" % layer)).read_text().splitlines():
        if not line.startswith("p2 "):
            continue
        head, rest = line.split("|", 1)
        key = tuple(int(x) for x in head.split()[1:3])
        parts = re.split(r"(\d+):", rest)
        for i in range(1, len(parts) - 1, 2):
            table[(key, int(parts[i]))] = tuple(int(x) for x in parts[i + 1].split())
    return table


t0, t1 = load(0), load(1)
keys = sorted({k for k, _ in t0} | {k for k, _ in t1})
print("puncture sets on layer 0: %d, on layer 1: %d, rows on each layer: %d" % (len({k for k, _ in t0}), len({k for k, _ in t1}), len(t0)))
bad = 0
for budget in (30, 31, 32, 33):
    rows0 = Counter(t0[(k, budget)] for k in keys)
    rows1 = Counter(t1[(k, budget)] for k in keys)
    most0 = max(sum(1 for x in r if x) for r in rows0)
    most1 = max(sum(1 for x in r if x) for r in rows1)
    same = sum(1 for k in keys if t0[(k, budget)] == t1[(k, budget)])
    bad += len(keys) - same
    print("budget %d : layer 0 maximum directions %d, different rows %d : layer 1 maximum directions %d, different rows %d : sets with the same row on the two layers %d of %d" % (
        budget, most0, len(rows0), most1, len(rows1), same, len(keys)))
print("sets with a different row on the two layers: %d" % bad)
