#!/usr/bin/env python3

import sys
try:
    import tomllib
except:
    import tomli as tomllib
import struct
import io
import re

POSITIVE_INT = r'[1-9][0-9]*'
NONNEG_INT = r'(0|([1-9][0-9]*))'

def usage():
    print(f"Usage: {sys.argv[0]} input.txt [subtask_name]", file=sys.stderr)
    exit(1)

def run(f, st):
    for k, v in subtask[st].items():
        globals()[k] = v

    data = f.readlines()
    assert len(data) > 0

    # dumb workaround because sample can't be special
    if data == ['1\n', '5\n', '4 2 0 1 3\n']: return
    
    assert re.match(r'^-47\n$', data[0])
    data = data[1:]
    assert len(data) > 0

    assert re.match(rf'^{NONNEG_INT}\n$', data[0])
    subt = int( data[0] )
    assert 1 <= subt <= 4
    if subt == 1: assert st == 'n_is_8'
    if subt == 2: assert st == 'zero_one_friends'
    if subt == 3: assert st == 'n_is_medium'
    if subt == 4: assert st == 'n_is_large'
    data = data[1:]
    assert len(data) > 0

    assert re.match(rf'^{POSITIVE_INT}\n$', data[0])
    tcs = int( data[0] )
    assert MINT <= tcs <= MAXT
    assert len(data) == 1 + 2*tcs

    for tc in range(tcs):
        tcdata = data[1+2*tc : 3+2*tc]

        assert re.match(rf'^{POSITIVE_INT} {NONNEG_INT}\n$', tcdata[0])
        n, det = [ int(_) for _ in tcdata[0].split() ]
        assert MINN <= n <= MAXN
        assert 0 <= det <= 2

        if st == 'zero_one_friends':
            assert det == 1

        if det:
            assert re.match(rf'^{NONNEG_INT}( {NONNEG_INT})*\n$', tcdata[1])
            arr = [ int(_) for _ in tcdata[1].split() ]
            assert list(sorted(arr)) == list(range(n))
            if st == 'zero_one_friends':
                i = arr.index(0)
                j = arr.index(1)
                assert abs(i-j) == 1
        else:
            assert re.match(r'^[a-z0-9_]+\n$', tcdata[1])

if __name__ == "__main__":
    if len(sys.argv) < 2:
        usage()

    st = sys.argv[2]

    with open("gen.toml", "rb") as gen_toml:
        gen_toml = tomllib.load(gen_toml)
        global_variables = globals()
        global_variables |= gen_toml
        print(subtask)
        global_variables |= subtask[st]

    f = open(sys.argv[1], "r")
    run(f, st)
