#!/usr/bin/env python3
import collections
import lzma
import sys


def read_dimacs(path):
    opener = lzma.open if path.endswith(".xz") else open
    with opener(path, "rt") as f:
        n = m = None
        clauses = []
        for line in f:
            if not line or line[0] == "c":
                continue
            if line[0] == "p":
                parts = line.split()
                n, m = int(parts[2]), int(parts[3])
                continue
            lits = [int(x) for x in line.split()]
            if lits and lits[-1] == 0:
                lits.pop()
            if lits:
                clauses.append(tuple(lits))
    return n, m, clauses


def main(paths):
    for path in paths:
        n, m, clauses = read_dimacs(path)
        sizes = collections.Counter(len(c) for c in clauses)
        pos = neg = 0
        deg = [0] * (n + 1)
        units = []
        for c in clauses:
            for lit in c:
                if lit > 0:
                    pos += 1
                else:
                    neg += 1
                deg[abs(lit)] += 1
            if len(c) == 1:
                units.append(c[0])
        active = sum(1 for d in deg[1:] if d)
        print(path.split("/")[-1])
        print("  vars clauses active", n, m, active)
        print("  sizes", " ".join(f"{k}:{sizes[k]}" for k in sorted(sizes)))
        print("  lits pos neg", pos, neg, "ratio", round(pos / max(1, pos + neg), 4))
        print("  units", len(units), units[:40])
        print("  degree min/max/avg", min(d for d in deg[1:] if d), max(deg), round(sum(deg)/active, 2))
        print("  top degrees", sorted(((d, i) for i, d in enumerate(deg) if i), reverse=True)[:10])


if __name__ == "__main__":
    main(sys.argv[1:])
