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


def open_formula(path):
    return lzma.open(path, "rt") if path.endswith(".xz") else open(path, "rt")


def profile(path):
    vars_ = clauses_ = 0
    lengths = collections.Counter()
    pos = neg = 0
    long_clause_count = 0
    with open_formula(path) as fh:
        for line in fh:
            if not line or line[0] == "c":
                continue
            if line[0] == "p":
                parts = line.split()
                vars_, clauses_ = int(parts[2]), int(parts[3])
                continue
            lits = [int(x) for x in line.split() if x != "0"]
            if not lits:
                continue
            lengths[len(lits)] += 1
            long_clause_count += len(lits) > 2
            pos += sum(x > 0 for x in lits)
            neg += sum(x < 0 for x in lits)
    return {
        "file": os.path.basename(path),
        "vars": vars_,
        "clauses": clauses_,
        "lengths": dict(sorted(lengths.items())),
        "long_clauses": long_clause_count,
        "positive_literals": pos,
        "negative_literals": neg,
    }


def main():
    for path in sys.argv[1:]:
        print(profile(path))


if __name__ == "__main__":
    main()
