Submission details
Task:Tulkki
Sender:henri0
Submission time:2025-10-31 16:03:52 +0200
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.00 s1, 2, 3details
#20.00 s1, 2, 3details
#30.00 s1, 2, 3details
#40.00 s1, 2, 3details
#50.01 s1, 2, 3details
#60.01 s1, 2, 3details
#70.00 s2, 3details
#80.00 s2, 3details
#90.00 s2, 3details
#100.00 s2, 3details
#110.00 s2, 3details
#120.00 s2, 3details
#130.00 s3details
#140.00 s3details
#150.01 s3details
#160.00 s3details
#170.04 s3details
#180.01 s3details

Compiler report

input/code.cpp: In function 'void PrintCommands(const std::vector<Command>&, size_t)':
input/code.cpp:95:27: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for (int i = 0; i < depth; ++i) {
      |                         ~~^~~~~~~
input/code.cpp: In member function 'void Program::RunCommands(const std::vector<Command>&)':
input/code.cpp:152:39: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  152 |                     for (int i = 0; i < loops; ++i) {
      |                                     ~~^~~~~~~

Code

#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <sstream>
#include <cstdint>
#include <sstream>

enum CommandType : uint8_t {
    Clear = 0,
    Increase,
    Print,
    Repeat
};

struct Command {
    CommandType type;
    size_t variable;
    std::vector<Command> subCommands;
};

size_t FindEndBracket(const std::string& str, size_t start) {
    size_t count = 0;
    for (size_t i = start; i < str.length(); ++i) {
        if (str[i] == '(') {
            ++count;
        } else if (str[i] == ')') {
            if (count == 0) {
                return i;
            }
            --count;
        }
    }
    return std::string::npos;
}

std::vector<Command> ProcessInput(std::string str) {
    std::vector<Command> commands;

    for (size_t i = 0; i < str.length(); ++i) {
        if (i >= str.length()) break;
        while (str[i] == ' ' && (i + 1 < str.length() && str[i+1]==' ')) {
            str.erase(i, 1);
        }
    }

    //std::cout << str << std::endl;

    std::stringstream ss(str);
    std::string commandToken;
    std::string varToken;
    while (std::getline(ss, commandToken, ' ')) {

        if (commandToken == "" || commandToken.find(" ") != std::string::npos) continue;

        std::getline(ss, varToken, ' ');
        //std::cout << "token: " << commandToken << std::endl;
        //std::cout << "var: " << (size_t)varToken[0] - (size_t)'A' << std::endl;
        size_t var = (size_t)varToken[0] - (size_t)'A';
        if (commandToken == "CLEAR") {
            commands.push_back({CommandType::Clear, var});
        } else if (commandToken == "INCREASE") {
            commands.push_back({CommandType::Increase, var});
        } else if (commandToken == "PRINT") {
            commands.push_back({CommandType::Print, var});
        } else if (commandToken == "REPEAT") {
            std::string s;
            std::getline(ss, s, ' ');
            std::getline(ss, s, ' ');
            
            Command command;
            command.type = CommandType::Repeat;
            command.variable = var;

            size_t pos = ss.tellg();
            size_t end = FindEndBracket(str, pos);
            //std::cout << "l: " << str.rfind(")") << std::endl;
            //std::cout << str.substr(pos, str.rfind(")")-pos) << std::endl;

            command.subCommands = ProcessInput(str.substr(pos, end-pos));

            commands.push_back(std::move(command));

            ss.seekg(end+1);

            //break;
        }
    }

    return commands;
}

void PrintCommands(const std::vector<Command>& commands, size_t depth = 0) {
    for (const auto& cmd : commands) {
        for (int i = 0; i < depth; ++i) {
            std::cout << "  ";
        }
        if (cmd.type == CommandType::Repeat) {
            std::cout << "Repeat " << cmd.variable << std::endl;
            PrintCommands(cmd.subCommands, depth + 1);
            continue;
        }
        switch (cmd.type) {
            case CommandType::Clear:
                std::cout << "Clear ";
                break;
            case CommandType::Increase:
                std::cout << "Increase ";
                break;
            case CommandType::Print:
                std::cout << "Print ";
                break;
            default:
                std::cout << "Unknown " << cmd.type << " ";
        }
        std::cout << cmd.variable;
        std::cout << std::endl;
    }
}

class Program {

    using VariableArray = std::array<int, 26>;

public:

    Program(const std::string &code) {
        m_variables.fill(0);
        m_commands = ProcessInput(code);
    }

    void Run() {
        RunCommands(m_commands);
    }

protected:

    void RunCommands(const std::vector<Command>& commands) {
        for (const auto& cmd : commands) {
            switch (cmd.type) {
                case CommandType::Clear:
                    m_variables[cmd.variable] = 0;
                    break;
                case CommandType::Increase:
                    ++m_variables[cmd.variable];
                    break;
                case CommandType::Print:
                    std::cout << m_variables[cmd.variable] << " ";
                    break;
                case CommandType::Repeat:
                    const size_t loops = m_variables[cmd.variable];
                    for (int i = 0; i < loops; ++i) {
                        RunCommands(cmd.subCommands);
                    }
                    break;
            }
        }
    }

private:

    std::vector<Command> m_commands;

    VariableArray m_variables;

};

int main() {

    std::string input;
    std::string line;

    while (std::getline(std::cin, line)) {
        if (line[0] == '#') continue;
        for (size_t i = 0; i < line.length(); ++i) {
            if (line[i] == '#') {
                line.erase(i);
                break;
            }
        }
        input += line + ' ';
    }
    std::cout << input << std::endl;
    PrintCommands(ProcessInput(input));

    Program program(input);

    program.Run();

//    std::cout << input << std::endl;

    std::cout << std::endl;

    return 0;
}

Test details

Test 1 (public)

Group: 1, 2, 3

Verdict:

input
PRINT X
INCREASE X
PRINT X
INCREASE X
PRINT X
...

correct output
0 1 2 0 

user output
PRINT X INCREASE X PRINT X INC...

Feedback: Output is longer than expected

Test 2 (public)

Group: 1, 2, 3

Verdict:

input
INCREASE
X
# aybabtu
   PRINT    X
INCREASE # test
...

correct output
1 3 

user output
INCREASE X    PRINT    X INCRE...

Feedback: Output is longer than expected

Test 3 (public)

Group: 1, 2, 3

Verdict:

input
# Create number 3
INCREASE X
INCREASE X
INCREASE X

...

correct output

user output
INCREASE X INCREASE X INCREASE...

Feedback: Output is longer than expected

Test 4 (public)

Group: 1, 2, 3

Verdict:

input
INCREASE A
PRINT A
INCREASE B
PRINT B
INCREASE C
...

correct output
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

user output
INCREASE A PRINT A INCREASE B ...

Feedback: Output is longer than expected

Test 5 (public)

Group: 1, 2, 3

Verdict:

input
INCREASE X
INCREASE X
INCREASE X
INCREASE X
INCREASE X
...

correct output
999 

user output
INCREASE X INCREASE X INCREASE...

Feedback: Output is longer than expected

Test 6 (public)

Group: 1, 2, 3

Verdict:

input
PRINT X
PRINT X
PRINT X
PRINT X
PRINT X
...

correct output
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

user output
PRINT X PRINT X PRINT X PRINT ...

Feedback: Output is longer than expected

Test 7 (public)

Group: 2, 3

Verdict:

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
5 5 5 5 5 

user output
INCREASE A INCREASE A INCREASE...

Feedback: Output is longer than expected

Test 8 (public)

Group: 2, 3

Verdict:

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
0 0 0 0 0 

user output
INCREASE A INCREASE A INCREASE...

Feedback: Output is longer than expected

Test 9 (public)

Group: 2, 3

Verdict:

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
6 7 8 9 10 

user output
INCREASE A INCREASE A INCREASE...

Feedback: Output is longer than expected

Test 10 (public)

Group: 2, 3

Verdict:

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
5 5 

user output
INCREASE A INCREASE A INCREASE...

Feedback: Output is longer than expected

Test 11 (public)

Group: 2, 3

Verdict:

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
20 

user output
INCREASE A INCREASE A INCREASE...

Feedback: Output is longer than expected

Test 12 (public)

Group: 2, 3

Verdict:

input
INCREASE A
INCREASE A

INCREASE B
INCREASE B
...

correct output
42 

user output
INCREASE A INCREASE A  INCREAS...

Feedback: Output is longer than expected

Test 13 (public)

Group: 3

Verdict:

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 

user output
INCREASE A INCREASE A INCREASE...

Feedback: Output is longer than expected

Test 14 (public)

Group: 3

Verdict:

input
# Create number 3
INCREASE A INCREASE A INCREASE...

correct output
12 

user output
INCREASE A INCREASE A INCREASE...

Feedback: Output is longer than expected

Test 15 (public)

Group: 3

Verdict:

input
INCREASE X
INCREASE X
INCREASE X
INCREASE X
INCREASE X
...

correct output
531441 

user output
INCREASE X INCREASE X INCREASE...

Feedback: Output is longer than expected

Test 16 (public)

Group: 3

Verdict:

input
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A
...

correct output
1337 

user output
INCREASE A INCREASE A INCREASE...

Feedback: Output is longer than expected

Test 17 (public)

Group: 3

Verdict:

input
INCREASE A
INCREASE A

REPEAT A TIMES (
    REPEAT A TIMES (
...

correct output
1 2 1 2 1 1 3 4 3 4 3 4 3 4 3 ...

user output
INCREASE A INCREASE A  REPEAT ...

Feedback: Output is longer than expected

Test 18 (public)

Group: 3

Verdict:

input
# Efficient algorithm for find...

correct output
2 3 5 7 11 13 17 19 23 29 31 3...

user output
INCREASE V INCREASE X INCREASE...

Feedback: Output is longer than expected