#!/opt/local/bin/python

import random, collections, string, copy

probname = "whattimeisitmrfox"

tcount = {}
total_secret = 0

# bounds like MAXN here
MAXN = 200000
MAXM = 200000
MAXC = 20
MAXL = 100000000

def gen(s, calls, L, intype = probname):
    global total_secret

    assert len(s) in range(1,MAXN+1) and type(s) == list
    assert L in range(2, MAXL+1, 2)

    for v in s:
        assert len(v) == 11 and type(v) == list
        for x in v: assert x in range(1, MAXL+1)
    
    assert len(calls) in range(1,MAXM+1) and type(calls) == list
    tcalls = 0
    for c in calls:
        assert c in range(1,13)
        if c == 12: tcalls += 1
    assert tcalls <= MAXC and calls[-1] == 12

    path = "../data"

    if intype not in tcount: tcount[intype] = 0
    tcount[intype] += 1

    if intype == "sample":
        filename = "{0}/sample/{1}.in".format(path, tcount[intype])
    else:
        filename = "{0}/secret/{3:02}-{1}-{2}.in".format(path, intype, tcount[intype], total_secret)
        total_secret += 1

    print("Generating:", filename)

    out = open(filename, "w")

    # print the test case here, remember to print to file=out
    print(len(s), len(calls), L, file=out)
    for v in s: print(*v, file=out)
    print(*calls, sep="\n", file=out)

    out.close()

def gensample():
    s1 = [1]*11
    s2 = [1,2]*5+[1]
    s3 = [3]*11

    gen([s1, s2], [4,1,12,6,6,12], 16, "sample")
    gen([s1, s2], [2,4,10,12], 10, "sample")
    gen([s1, s2, s3], [10,12,10,10,12],20,"sample")

def gencorner():
    s1 = [1]*11
    gen([s1], [12], 10, "corner")
    gen([s1], [12,1,12], 10, "corner")
    gen([s1], [12,5,12,11,11,12], 10, "corner")
    gen([s1], [12,12], 10, "corner")
    gen([s1], [1,12,2,12,3,12,4,12,5,12,6,12], 10, "corner")
    gen([[MAXL]*11], [1,12,2,12,12], MAXL, "corner")
    gen([[MAXL//2]*11], [1,12], MAXL, "corner")
    gen([[MAXL//4]*11], [1,12,3,12], MAXL, "corner")
    gen([[MAXL]*11], [11]*(10000-1)+[12], MAXL, "corner")

def genmidway():
    S = [1]*11
    gen([S]*2, [1]*10+[12], 20, "midway")

def genrand(N,M,L,C):
    s = []
    
    def fn(i):
        return (0.5*(N-i)+i*5)/N

    for i in range(N):
        t = [random.randint(1,max(1,L//int(fn(i)*M/C*j))) for j in range(1,12)]
        s.append(t)
    
    calls = [12]*(C-1)
    for i in range(M-C): calls.append(random.randint(1,11))
    random.shuffle(calls)
    calls.append(12)

    random.shuffle(s)

    gen(s,calls,L,"rand")
    

if __name__ == "__main__":
    random.seed(0)

    gensample()

    gencorner()

    genrand(10,MAXM,MAXL,1)
    genrand(10,MAXM,MAXL,2)
    genrand(10,MAXM,MAXL,10)
    genrand(1,MAXM,MAXL,MAXC)
    genrand(10,MAXM,MAXL,MAXC)
    genrand(100,MAXM,MAXL,MAXC)
    genrand(MAXN,MAXM,MAXL,MAXC)
    genrand(MAXN,MAXM,MAXL,MAXC)

    genmidway()
