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

def op(p): return lzma.open(p,'rt') if p.endswith('.xz') else open(p)
for path in sys.argv[1:]:
 t=time.time(); rows=[]; bins=[]
 with op(path) as f:
  for line in f:
   if line[0]=='c' or line[0]=='p': continue
   arr=[int(x) for x in line.split() if x!='0']
   if len(arr)==2: bins.append(tuple(arr))
   elif arr: rows.append(arr)
 rowof={}; idx={}; sel={}
 for r,row in enumerate(rows):
  for i,lit in enumerate(row): rowof[abs(lit)]=r; idx[abs(lit)]=i; sel[abs(lit)]=lit
 R=len(rows); dom=[(1<<len(row))-1 for row in rows]
 # forbidden masks forb[(r,s)][i] bit j forbidden
 forb={}
 for a,b in bins:
  va,vb=abs(a),abs(b); r,s=rowof[va],rowof[vb]
  if r==s: continue
  i,j=idx[va],idx[vb]
  forb.setdefault((r,s), [0]*len(rows[r]))[i] |= 1<<j
  forb.setdefault((s,r), [0]*len(rows[s]))[j] |= 1<<i
 neigh=[set() for _ in rows]
 for r,s in forb: neigh[r].add(s)
 q=collections.deque(list(forb.keys())); it=0; empty=False
 while q:
  r,s=q.popleft(); it+=1
  masks=forb[(r,s)]; ds=dom[s]; dr=dom[r]; new=dr
  fulls=(1<<len(rows[s]))-1
  for i in range(len(rows[r])):
   if dr>>i &1:
    allowed = ds & (~masks[i] & fulls)
    if allowed==0: new &= ~(1<<i)
  if new!=dr:
   dom[r]=new
   if new==0: empty=True; break
   for u in neigh[r]:
    if u!=s: q.append((u,r))
 print(path.split('/')[-1], 'R',R,'empty',empty,'domains left',sum(d.bit_count() for d in dom),'iters',it,'time',round(time.time()-t,2))
