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

def read(path, maxclauses=None):
    f = lzma.open(path,'rt') if path.endswith('.xz') else open(path)
    nvar=ncl=None; clauses=[]; comments=[]
    for line in f:
        if line.startswith('c'):
            comments.append(line.strip()); continue
        if line.startswith('p'):
            _,_,nvar,ncl=line.split()[:4]; nvar=int(nvar); ncl=int(ncl); continue
        if not line.strip(): continue
        lits=list(map(int,line.split()))
        if lits and lits[-1]==0: lits=lits[:-1]
        clauses.append(lits)
        if maxclauses and len(clauses)>=maxclauses: break
    return nvar,ncl,comments,clauses

for path in sys.argv[1:]:
    nvar,ncl,comments,clauses=read(path)
    sizes=collections.Counter(map(len,clauses))
    units=[c[0] for c in clauses if len(c)==1]
    prefix=[]
    for c in clauses:
        if len(c)==1 and c[0]<0: prefix.append(-c[0])
        else: break
    n_hint=None; w_hint=None
    for c in comments:
        m=re.search(r'w = (\d+)',c); 
        if m: w_hint=int(m.group(1))
        m=re.search(r'n = (\d+)',c)
        if m: n_hint=int(m.group(1))
    print(os.path.basename(path), 'vars',nvar,'clauses',ncl,'sizes',dict(sorted(sizes.items())),'n',n_hint,'w',w_hint,'prefixneg',len(prefix), (min(prefix),max(prefix)) if prefix else None)
    # variable occurrences summary ranges
    occ=collections.Counter(abs(l) for c in clauses for l in c)
    print(' occ maxvar', max(occ) if occ else None, 'top', occ.most_common(5), 'once', sum(1 for v in range(1,nvar+1) if occ[v]==1), 'zero', sum(1 for v in range(1,nvar+1) if occ[v]==0))
