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

def read(path):
    op = lzma.open if path.endswith('.xz') else open
    n=m=0; sizes=collections.Counter(); pos=neg=0; clauses=[]
    with op(path,'rt') as f:
        for line in f:
            if not line or line[0] in 'c%0\n': continue
            if line[0]=='p':
                _,_,n,m=line.split()[:4]; n=int(n); m=int(m); continue
            lits=list(map(int,line.split()))
            if lits and lits[-1]==0: lits=lits[:-1]
            if not lits: continue
            clauses.append(lits); sizes[len(lits)]+=1
            for l in lits:
                if l>0: pos+=1
                else: neg+=1
    def sat_const(val):
        # val False/True for all vars
        return sum(any((l>0)==val for l in c) for c in clauses)
    print(os.path.basename(path), 'n',n,'m',len(clauses),'sizes',dict(sorted(sizes.items())),'pos',pos,'neg',neg,'all0_unsat',len(clauses)-sat_const(False),'all1_unsat',len(clauses)-sat_const(True))
    # fingerprint groups of 4 ternary clauses with same abs vars
    groups=collections.defaultdict(list)
    for i,c in enumerate(clauses):
        if len(c)==3:
            groups[tuple(sorted(map(abs,c)))].append(tuple(1 if x>0 else 0 for x in c))
    cnt=collections.Counter(len(v) for v in groups.values())
    print('ternary abs group sizes',cnt.most_common(10),'groups4',sum(1 for v in groups.values() if len(v)==4))
    print('units sample',[c[0] for c in clauses if len(c)==1][:20])

for p in sys.argv[1:]: read(p)
