#!/usr/bin/env python3
import glob,lzma,os,subprocess,tempfile,time
files=sorted(glob.glob('/data/train/*.xz'))
solved=unknown=crash=0
for f in files:
    with tempfile.TemporaryDirectory() as d:
        cnf=os.path.join(d,'f.cnf'); out=os.path.join(d,'out.pbp'); model=os.path.join(d,'model.txt')
        with lzma.open(f,'rb') as src, open(cnf,'wb') as dst: dst.write(src.read())
        t=time.time()
        p=subprocess.run(['/workspace/solver/solver',cnf,out],stdout=open(model,'w'),stderr=subprocess.PIPE,timeout=140)
        dt=time.time()-t
        txt=open(model).read()
        key=''
        for line in txt.splitlines():
            if line.startswith('c key='): key=line.split()[1]
        if p.returncode==10: solved+=1; st='sat'
        elif p.returncode==0: unknown+=1; st='unknown'
        else: crash+=1; st='crash'
        print(f'{st:7} {dt:6.2f}s {key:14} {os.path.basename(f)}', flush=True)
print('TOTAL',solved,unknown,crash)
