import json

TASK_ROOT = '/sandbox/project/task/'
FAILED_TEST_BEGIN = '#educational_plugin FAILED + '
FAILED_TEST_CONTINUE = '#educational_plugin'

if __name__ == '__main__':
    score = 1
    feedback = ''

    code = open('code.txt').read().strip()
    if code != '0':
        stdout = open('stdout.txt').read().splitlines()
        stderr = open('stderr.txt').read().splitlines()
        score = 0
        feedback = ('Cannot check the submission. Please send the report to Hyperskill team.\n'
                    'stdout:\n{stdout}\n\nstderr:\n{stderr}'.format(stdout='\n'.join(stdout),
                                                                    stderr='\n'.join(stderr)))

        if any(line.startswith('> Compilation failed;')
               for line in stderr):
            output = []

            for line in stderr:
                if line.startswith('FAILURE'):
                    break

                if line.startswith(TASK_ROOT):
                    line = line.replace(TASK_ROOT, '', 1)

                output.append(line)

            feedback = 'Compilation error\n' + '\n'.join(output).strip()

        else:
            if any(line.startswith(FAILED_TEST_BEGIN) for line in stdout):
                output = []
                output_started = False

                for line in stdout:
                    if output_started and line.startswith(FAILED_TEST_CONTINUE):
                        output.append(line[len(FAILED_TEST_CONTINUE):])

                    if not output_started and line.startswith(FAILED_TEST_BEGIN):
                        output_started = True
                        output.append(line[len(FAILED_TEST_BEGIN):])

                feedback = '\n'.join(output).strip()

    result = {
        'score': score,
        'feedback': feedback,
    }
    print(json.dumps(result))
