Submission details
Task:Tulkki
Sender:rottis
Submission time:2025-10-27 09:01:20 +0200
Language:C++ (C++17)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED12
#2ACCEPTED32
#3ACCEPTED56
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3details
#2ACCEPTED0.00 s1, 2, 3details
#3ACCEPTED0.00 s1, 2, 3details
#4ACCEPTED0.00 s1, 2, 3details
#5ACCEPTED0.00 s1, 2, 3details
#6ACCEPTED0.01 s1, 2, 3details
#7ACCEPTED0.00 s2, 3details
#8ACCEPTED0.00 s2, 3details
#9ACCEPTED0.00 s2, 3details
#10ACCEPTED0.00 s2, 3details
#11ACCEPTED0.00 s2, 3details
#12ACCEPTED0.00 s2, 3details
#13ACCEPTED0.00 s3details
#14ACCEPTED0.00 s3details
#15ACCEPTED0.01 s3details
#16ACCEPTED0.00 s3details
#17ACCEPTED0.05 s3details
#18ACCEPTED0.01 s3details

Compiler report

input/code.cpp: In function 'void execute()':
input/code.cpp:101:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |         while (instruction_pointer < nodes.size()) {
      |                ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
input/code.cpp:119:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |                 if (instruction_pointer + 1 >= nodes.size()) {
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
input/code.cpp:129:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |                 if (instruction_pointer + 1 >= nodes.size()) {
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
input/code.cpp:137:45: warning:...

Code

// TODO optimointi

#include <iostream>
#include <string>
#include <vector>




enum InstructionType {
    CLEAR,
    INCREMENT,
    PRINT,
    REPEAT,
    TIMES,
    VARIABLE,
    BRACKET_LEFT,
    BRACKET_RIGHT,
    NOOP
};

typedef struct Node {
    char type;
    char value;
} Instruction;



inline Node convert_string(std::string str) {
    if (str == "PRINT") {
        return Node {InstructionType::PRINT, 0};
    } else if (str == "INCREASE") {
        return Node {InstructionType::INCREMENT, 0};
    } else if (str == "CLEAR") {
        return Node {InstructionType::CLEAR, 0};
    } else if (str == "REPEAT") {
        return Node {InstructionType::REPEAT, 0};
    } else if (str == "TIMES") {
        return Node {InstructionType::TIMES, 0};
    } else if (str == "(") {
        return Node {InstructionType::BRACKET_LEFT, 0};
    } else if (str == ")") {
        return Node {InstructionType::BRACKET_RIGHT, 0};
    } else if (str[0] >= 'A' && str[0] <= 'Z') {
        return Node {InstructionType::VARIABLE, str[0]};
    } else {
        return Node {InstructionType::NOOP, 0};
    }
}

inline bool is_whitespace(char c) {
    return (c == ' ' || c == '\n' || c == '\t');
}

int variables[26];

void execute() {
    for (int i = 0; i < 26; i++) {
        variables[i] = 0;
    }

    std::string c_line;
    std::string buf;
    std::vector<Node> nodes;
    bool flush_queued = false;

    // set to true if there is a loop with 0 iterations, just skips all nodes until finds the right bracket
    bool skip_loop = false;
    int skip_loop_incomplete_brackets_left = 0;
    
    int instruction_pointer = 0;
    int current_loop_depth = 0;

    int loop_begin_addresses[1000];
    int loop_iters_left[1000];

    while (std::getline(std::cin, c_line)) {

        c_line.push_back('\n'); // trailing whitespace to process entire line

        for (unsigned int i = 0; i < c_line.size(); i++) {
            char c = c_line[i];

            if (is_whitespace(c) || c == '#') {
                Node node = convert_string(buf);
                if (node.type != InstructionType::NOOP) {
                    //std::cout << "Parsed input: \"" << buf << "\"" << std::endl;
                    nodes.push_back(node);
                }

                buf = "";
            } else {
                buf.push_back(c);
            }
            
            if (c == '#') {
                break;
            }
        }

        while (instruction_pointer < nodes.size()) {
            Node instruction = nodes[instruction_pointer];

            if (skip_loop) {
                if (instruction.type == InstructionType::BRACKET_LEFT) {
                    skip_loop_incomplete_brackets_left++;
                } if (instruction.type == InstructionType::BRACKET_RIGHT) {
                    skip_loop_incomplete_brackets_left--;
                    if (skip_loop_incomplete_brackets_left <= 0) {
                        skip_loop = false;
                        current_loop_depth -= 1;
                    }
                }
                instruction_pointer++;
                continue;
            }

            if (instruction.type == InstructionType::PRINT) {
                if (instruction_pointer + 1 >= nodes.size()) {
                    break;
                }
                //std::cout << "Printing " << nodes[instruction_pointer + 1].value << std::endl;
                std::cout << variables[nodes[instruction_pointer + 1].value - 'A'] << ' ';
    
                flush_queued = true;
                instruction_pointer += 2;

            } else if (instruction.type == InstructionType::INCREMENT) {
                if (instruction_pointer + 1 >= nodes.size()) {
                    break;
                }
                //std::cout << "Incrementing " << nodes[instruction_pointer + 1].value << std::endl;
                variables[nodes[instruction_pointer + 1].value - 'A']++;
                instruction_pointer += 2;

            } else if (instruction.type == InstructionType::CLEAR) {
                if (instruction_pointer + 1 >= nodes.size()) {
                    break;
                }
                //std::cout << "Clearing " << nodes[instruction_pointer + 1].value << std::endl;
                variables[nodes[instruction_pointer + 1].value - 'A'] = 0;
                instruction_pointer += 2;

            } else if (instruction.type == InstructionType::REPEAT) {
                if (instruction_pointer + 1 >= nodes.size()) {
                    break;
                }
                current_loop_depth += 1;
                loop_iters_left[current_loop_depth] = variables[nodes[instruction_pointer + 1].value - 'A'];
                if (loop_iters_left[current_loop_depth] == 0) {
                    skip_loop = true;
                }
                // one loop means we skip back to beginning zero times 
                loop_iters_left[current_loop_depth]--;
                instruction_pointer += 2;

            } else if (instruction.type == InstructionType::TIMES) {
                //std::cout << "Beginning loop: depth: " << current_loop_depth << ", iters left: " <<
                //loop_iters_left[current_loop_depth] << std::endl;
                
                loop_begin_addresses[current_loop_depth] = instruction_pointer + 2;
                instruction_pointer += 2;

            } else if (instruction.type == InstructionType::BRACKET_RIGHT) {
                
                if (loop_iters_left[current_loop_depth] > 0) {
                    //std::cout << "Looping again, depth= " << current_loop_depth << std::endl;
                    instruction_pointer = loop_begin_addresses[current_loop_depth];
                    loop_iters_left[current_loop_depth]--;

                } else { // done with loop
                    //std::cout << "Done with loop " << current_loop_depth << std::endl;
                    current_loop_depth--;
                    instruction_pointer++;
                }
                
            } else {
                instruction_pointer++;
            }
        }
        if (flush_queued) {
            std::cout.flush();
        }
    }
}

/*
   PRINT        X
INCREASE X
PRINT X
INCREASE X
PRINT X
CLEAR X
PRINT X
*/

/*
INCREASE A
INCREASE A
INCREASE A
INCREASE A
INCREASE A

REPEAT A TIMES (
    INCREASE A
    PRINT A
)
*/

/*
# Create number 3
INCREASE A INCREASE A INCREASE A

# Create number 4
INCREASE B INCREASE B INCREASE B INCREASE B

# Calculate 3 * 4
REPEAT A TIMES ( REPEAT B TIMES ( INCREASE C ) )

# Print result
PRINT C
*/

/*
INCREASE
X
# aybabtu
   PRINT    X
INCREASE # test
X  INCREASE X PRINT#X
X
*/

int main(void) {
    execute();

    return 0;
}

Test details

Test 1 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

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

correct output
0 1 2 0 

user output
0 1 2 0 

Test 2 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

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

correct output
1 3 

user output
1 3 

Test 3 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

input
# Create number 3
INCREASE X
INCREASE X
INCREASE X

...

correct output

user output

Test 4 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

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
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

Test 5 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

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

correct output
999 

user output
999 

Test 6 (public)

Group: 1, 2, 3

Verdict: ACCEPTED

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
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

Test 7 (public)

Group: 2, 3

Verdict: ACCEPTED

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

correct output
5 5 5 5 5 

user output
5 5 5 5 5 

Test 8 (public)

Group: 2, 3

Verdict: ACCEPTED

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

correct output
0 0 0 0 0 

user output
0 0 0 0 0 

Test 9 (public)

Group: 2, 3

Verdict: ACCEPTED

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

correct output
6 7 8 9 10 

user output
6 7 8 9 10 

Test 10 (public)

Group: 2, 3

Verdict: ACCEPTED

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

correct output
5 5 

user output
5 5 

Test 11 (public)

Group: 2, 3

Verdict: ACCEPTED

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

correct output
20 

user output
20 

Test 12 (public)

Group: 2, 3

Verdict: ACCEPTED

input
INCREASE A
INCREASE A

INCREASE B
INCREASE B
...

correct output
42 

user output
42 

Test 13 (public)

Group: 3

Verdict: ACCEPTED

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
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 

Test 14 (public)

Group: 3

Verdict: ACCEPTED

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

correct output
12 

user output
12 

Test 15 (public)

Group: 3

Verdict: ACCEPTED

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

correct output
531441 

user output
531441 

Test 16 (public)

Group: 3

Verdict: ACCEPTED

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

correct output
1337 

user output
1337 

Test 17 (public)

Group: 3

Verdict: ACCEPTED

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
1 2 1 2 1 1 3 4 3 4 3 4 3 4 3 ...

Test 18 (public)

Group: 3

Verdict: ACCEPTED

input
# Efficient algorithm for find...

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

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