#!/usr/bin/env python3
import sys,lzma,collections,math
path=sys.argv[1]
f=lzma.open(path,'rt')
clauses=[]; nvar=0
long_sizes=collections.Counter()
for line in f:
    if line.startswith('c'): continue
    if line.startswith('p'):
        nvar=int(line.split()[2]); continue
    lits=list(map(int,line.split()))
    if lits and lits[-1]==0: lits=lits[:-1]
    clauses.append(lits); long_sizes[len(lits)]+=1
n=max(k for k,v in long_sizes.items() if k==v) # works
prim=n*n
cnt=collections.Counter(); examples={}
for c in clauses:
    if len(c)==2 and c[0]<0 and c[1]<0 and abs(c[0])<=prim and abs(c[1])<=prim:
        a,b=-c[0],-c[1]
        ra,ca=divmod(a-1,n); rb,cb=divmod(b-1,n); ca+=1; cb+=1
        if ra==rb: key=('same_row',abs(ca-cb))
        elif ca==cb: key=('same_col',abs(ra-rb))
        else: key=('edge?', abs(ca-cb), abs(ra-rb))
        cnt[key]+=1; examples.setdefault(key,(a,b,ra+1,ca,rb+1,cb))
print('n',n,'prim',prim,'vars',nvar,'clauses',len(clauses))
for k,v in cnt.most_common(30): print(k,v,examples[k])
print('edge diff counts', collections.Counter(k[1] for k in cnt for _ in range(cnt[k]) if k[0]=='edge?').most_common(20))
