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

def parse(path, maxclauses=None):
    opener=lzma.open if path.endswith('.xz') else open
    n=m=0; sizes=collections.Counter(); pos=neg=0; units=[]; deg=collections.Counter(); bindeg=0; horns=0; allneg=0; allpos=0; clauses=0; sample=[]
    with opener(path,'rt',errors='ignore') as f:
        lits=[]
        for line in f:
            if not line or line[0]=='c': continue
            if line[0]=='p':
                sp=line.split(); n=int(sp[2]); m=int(sp[3]); continue
            for tok in line.split():
                v=int(tok)
                if v==0:
                    clauses+=1; sizes[len(lits)]+=1
                    pc=sum(1 for x in lits if x>0); nc=len(lits)-pc
                    pos+=pc; neg+=nc
                    if pc<=1: horns+=1
                    if pc==0: allneg+=1
                    if nc==0: allpos+=1
                    if len(lits)==1: units.append(lits[0])
                    if len(lits)==2: bindeg+=1
                    for x in lits: deg[abs(x)]+=1
                    if len(sample)<5: sample.append(tuple(lits))
                    lits=[]
                    if maxclauses and clauses>=maxclauses: return locals()
                else: lits.append(v)
    return locals()

for p in sys.argv[1:]:
    d=parse(p)
    deg=d['deg']; vals=list(deg.values())
    print(os.path.basename(p))
    print(' p',d['n'],d['m'],'seen',d['clauses'],'sizes',dict(sorted(d['sizes'].items())[:20]))
    print(' lits pos/neg',d['pos'],d['neg'],'horn%',round(100*d['horns']/d['clauses'],1),'allneg',d['allneg'],'allpos',d['allpos'],'units',len(d['units']),'bin',d['bindeg'])
    print(' deg min/avg/max', min(vals or [0]), round(sum(vals)/len(vals),2) if vals else 0, max(vals or [0]), 'usedvars',len(vals),'sample',d['sample'][:3])
