import random
import math
random.seed("personal01")

class TestGen:
    def __init__(self):
        self.test_number = 0
        self.desc_file = open("../tests.desc", "w")
        self.MAXLEN = 20

    def generate_samples(self):
        self.print_test("0111", "subtask0", "Test from sample", 0, "preliminary")
        self.print_test("00", "subtask0", "Test from sample", 0, "preliminary")

    def generate_random_test(self, maxn, subtask):
        s = ""
        for i in range(maxn):
            s += chr(ord('0') + random.randint(0, 1))
        self.print_test(s, subtask, "Random test, n = %d" % (len(s)))

    def cut(self, s):
        if len(s) > self.MAXLEN:
            return s[0:17] + "..."
        else:
            return s

    def generate_first_group(self):
        self.print_test("1", "subtask1", "n = 1, s = 1")
        self.print_test("0", "subtask1", "n = 1, s = 1")
        self.print_test("11", "subtask1", "n = 2, s = 11")
        self.print_test("100", "subtask1", "n = 3, s = 100")
        self.print_test("111", "subtask1", "n = 3, s = 111")
        self.print_test("1111", "subtask1", "n = 4, s = 1111")
        self.print_test("1101", "subtask1", "n = 4, s = 1101")
        for test in range(10):
            self.generate_random_test(20, "subtask1")
        s = "1" * 20
        self.print_test(s, "subtask1", "n = 20, s = %s" % (self.cut(s)))
        s = "10" * 10
        self.print_test(s, "subtask1", "n = 20, s = %s" % (self.cut(s)))
        s = "1" * 10 + "0" * 10
        self.print_test(s, "subtask1", "n = 20, s = %s" % (self.cut(s)))

    def generate_random_test_hard(self, maxn, subtask):
        numbers = [1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111]
        length = 0
        s = ""
        while length + 2 <= maxn:
            digit = random.randint(0, 1)
            number = numbers[random.randint(0, len(numbers) - 1)]
            while length + 1 + len(str(number)) > maxn:
                number = numbers[random.randint(0, len(numbers) - 1)]
            length += len(str(number)) + 1
            s += str(digit) + str(number)
        self.print_test(s, subtask, "n = %d, s = %s" % (len(s), self.cut(s)))

    def generate_second_group(self):
        for test in range(10):
            self.generate_random_test(1000, "subtask2")
        s = "1" * 1000
        self.print_test(s, "subtask2", "n = 1000, s = %s" % (self.cut(s)))
        for test in range(10):
            self.generate_random_test_hard(1000, "subtask2")

    def generate_third_group(self):
        for test in range(10):
            self.generate_random_test(10**5, "subtask3")
        s = "1" * (10**5)
        self.print_test(s, "subtask3", "n = 100000, s = %s" % (self.cut(s)))
        for test in range(10):
            self.generate_random_test_hard(10**5, "subtask3")

    def print_test(self, s, subtask, description, points=0, folder="tests"):
        self.test_number += 1
        print("Generating test %d" % self.test_number)
        test_name = "../" + folder + "/{0:0=2d}".format(self.test_number)
        test_file = open(test_name, "w")
        print(len(s), file=test_file)
        print(s, file=test_file)
        print(test_name + ", folder=" + folder + ", subtask=" + subtask + ", points=" + str(points) + ", comment=\"" + description + "\"", file=self.desc_file)
        test_file.close()                                                                                             

    def generate_all_tests(self):
        maxn = 10**5

        self.generate_samples()
        self.test_number = 0

        #First group of tests
        self.generate_first_group()       #tests 01-##
        print("comment_subtask1=\"n &lt;= 20\"", file=self.desc_file)
        print("bonus_subtask1=20", file=self.desc_file)

        #Second group of tests
        self.generate_second_group()      #tests ##-##
        print("comment_subtask2=\"n &lt;= 1000\"", file=self.desc_file)
        print("bonus_subtask2=40", file=self.desc_file)
        
        #Third group of tests
        self.generate_third_group()       #tests ##-##
        print("comment_subtask3=\"n &lt;= 100000\"", file=self.desc_file)
        print("bonus_subtask3=40", file=self.desc_file)

        self.desc_file.close()

import os, shutil
if os.path.exists('../preliminary'):
    print('Directory preliminary already exists')
    shutil.rmtree('../preliminary', ignore_errors=False, onerror=None)
if os.path.exists('../tests'):
    print('Directory tests already exists')
    shutil.rmtree('../tests', ignore_errors=False, onerror=None)
try:
    os.makedirs('../preliminary')
    os.makedirs('../tests')
except OSError:                                                                                                                      
    pass

import time
start = time.time()
writer = TestGen()
writer.generate_all_tests()
finish = time.time()
print('All tests were generated. Elapsed time: ', finish - start)