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

def op(path):
    return lzma.open(path, "rt") if path.endswith(".xz") else open(path)

targets = set(sys.argv[2:])
seen = set()
cur_v=None; cur=[]
def flush():
    global cur_v, cur
    if not cur: return
    sizes=tuple(sorted(collections.Counter(len(c) for c in cur).items()))
    signs=collections.Counter((len(c), sum(x>0 for x in c)) for c in cur)
    sig=f"{sizes}|{tuple(sorted(signs.items()))}"
    if (not targets or sig in targets) and sig not in seen:
        seen.add(sig)
        print("SIG", sig, "var", cur_v, "group_len", len(cur))
        for c in cur[:20]:
            print(" ", " ".join(map(str,c)), "0")
        print()

with op(sys.argv[1]) as f:
    for line in f:
        if not line or line[0] in "cp": continue
        xs=[int(x) for x in line.split()]
        if xs and xs[-1]==0: xs.pop()
        if not xs: continue
        v=abs(xs[0])
        if cur and v!=cur_v:
            flush(); cur=[xs]; cur_v=v
        else:
            cur.append(xs); cur_v=v
flush()
