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

def read(path):
    f = lzma.open(path, 'rt') if path.endswith('.xz') else open(path)
    n=m=None; clauses=[]
    with f:
        cur=[]
        for line in f:
            if not line or line[0] in 'c%0': continue
            if line[0]=='p':
                _,_,n,m=line.split()[:4]; n=int(n); m=int(m); continue
            for tok in line.split():
                lit=int(tok)
                if lit==0:
                    if cur: clauses.append(tuple(cur)); cur=[]
                else: cur.append(lit)
    return n,m,clauses

for path in sys.argv[1:]:
    n,m,clauses=read(path)
    sizes=collections.Counter(map(len,clauses))
    pos=[0]*(n+1); neg=[0]*(n+1)
    for c in clauses:
        for l in c:
            (pos if l>0 else neg)[abs(l)] += 1
    deg=[pos[i]+neg[i] for i in range(1,n+1)]
    pure=sum(1 for i in range(1,n+1) if pos[i]==0 or neg[i]==0)
    units=[c[0] for c in clauses if len(c)==1]
    print(os.path.basename(path))
    print('vars',n,'clauses',len(clauses),'header',m,'sizes',dict(sorted(sizes.items())),'units',len(units),'pure',pure)
    print('deg min/avg/max',min(deg),sum(deg)/len(deg),max(deg))
    print('unit sample',units[:40])
    print('top degree',sorted([(deg[i-1],i,pos[i],neg[i]) for i in range(1,n+1)], reverse=True)[:20])
    print('first clauses',clauses[:20])
    print()
