import lzma,itertools,sys,collections
path=sys.argv[1]
cla=[]; n=0
for line in lzma.open(path,'rt'):
 if line[0]=='p': n=int(line.split()[2]); continue
 if line[0]=='c': continue
 c=[int(x) for x in line.split() if x!='0']
 if c: cla.append(c)
neg=set(); cand=[]
for idx,c in enumerate(cla):
 if len(c)==2 and c[0]<0 and c[1]<0:
  a,b=sorted([-c[0],-c[1]]); neg.add((a,b))
 elif 2<=len(c)<=192 and all(x>0 for x in c): cand.append(idx)
groups=[]; used=set(); first=None
for idx in cand:
 c=cla[idx]
 ok=not any(v in used for v in c) and all((min(a,b),max(a,b)) in neg for a,b in itertools.combinations(c,2))
 if ok:
  if first is None: first=idx
  groups.append(c); used.update(c)
primary=sum(1 for pc in range(first) if all(x>0 for x in cla[pc]))
print('primary',primary,'groups',len(groups),collections.Counter(map(len,groups)))
vg={v:i for i,g in enumerate(groups) for v in g}
occneg=collections.defaultdict(list)
for c in cla:
 for l in c:
  if l<0: occneg[-l].append(c)
for p in groups[0][0:1]+groups[8][0:1]:
 inc=sorted({vg[abs(l)] for c in occneg[p] for l in c if l>0 and vg.get(abs(l),-1)>=primary})
 print('p',p,'cell',vg[p],'inc',inc,[len(groups[g]) for g in inc], 'clauses',len(occneg[p]))
 cnt=0; examples=[]
 domains=[groups[g] for g in inc]
 for combo in itertools.product(*domains):
  val=set(combo); ok=True
  for c in occneg[p]:
   sat=False
   for l in c:
    if l==-p: continue
    if l>0 and l in val: sat=True; break
    if l<0 and -l not in val: sat=True; break
   if not sat: ok=False; break
  if ok:
   cnt+=1; examples.append(combo)
 print('allowed',cnt, examples[:10])
