Submission details
Task:Tulkki
Sender:rottis
Submission time:2025-10-27 01:15:41 +0200
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#1--1, 2, 3details
#2--1, 2, 3details
#3--1, 2, 3details
#4--1, 2, 3details
#5--1, 2, 3details
#6--1, 2, 3details
#7--2, 3details
#8--2, 3details
#9--2, 3details
#10--2, 3details
#11--2, 3details
#12--2, 3details
#13--3details
#14--3details
#15--3details
#16--3details
#17--3details
#18--3details

Compiler report

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

Code

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




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

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



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 {
        return Node {InstructionType::VARIABLE, str[0]};
    }
}

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

int variables[26];

/* CLEAR X (tyhjennys): asettaa muuttujan X arvoksi nollan */
void clear(Node node) {
    variables[node.value - 'A'] = 0;
}

/* INCREASE X (lisäys): kasvattaa muuttujan X arvoa yhdellä */
void increment(Node node) {
    variables[node.value - 'A']++;
}

/* PRINT X (tulostus): tulostaa muuttujan X arvon */
void print(Node node) {
    std::cout << variables[node.value - 'A'] << ' ';
}

/* REPEAT X TIMES ( ... ) (silmukka): suorittaa suluissa olevan koodin muuttujan X arvon verran kertoja */


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

    std::string c_line;
    std::string buf;
    std::vector<Node> nodes;
    
    // 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 instruction_pointer = 0;
    int current_loop_depth = 0;

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

    while (true) {
        std::getline(std::cin, c_line);
        c_line.push_back(' '); // trailing whitespace to process entire line

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

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

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

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

            if (instruction.type == InstructionType::PRINT) {
                print(nodes[instruction_pointer + 1]);
                instruction_pointer += 2;

            } else if (instruction.type == InstructionType::INCREMENT) {
                increment(nodes[instruction_pointer + 1]);
                instruction_pointer += 2;

            } else if (instruction.type == InstructionType::CLEAR) {
                clear(nodes[instruction_pointer + 1]);
                instruction_pointer += 2;

            } else if (instruction.type == InstructionType::REPEAT) {
                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) {
                loop_begin_addresses[current_loop_depth] = instruction_pointer + 2;

                instruction_pointer += 2;

            } else if (instruction.type == InstructionType::BRACKET_LEFT) {
                /* should never be here but oh well */
                instruction_pointer++;

            } else if (instruction.type == InstructionType::BRACKET_RIGHT) {
                
                if (loop_iters_left[current_loop_depth] > 0) {
                    instruction_pointer = loop_begin_addresses[current_loop_depth];
                    loop_iters_left[current_loop_depth]--;

                } else { // done with loop
                    instruction_pointer++;
                }
                
            }
        }
    }
}

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

int main(void) {
    execute();

    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
(empty)

Test 2 (public)

Group: 1, 2, 3

Verdict:

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

correct output
1 3 

user output
(empty)

Test 3 (public)

Group: 1, 2, 3

Verdict:

input
# Create number 3
INCREASE X
INCREASE X
INCREASE X

...

correct output

user output
(empty)

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
(empty)

Test 5 (public)

Group: 1, 2, 3

Verdict:

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

correct output
999 

user output
(empty)

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
(empty)

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
(empty)

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
(empty)

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
(empty)

Test 10 (public)

Group: 2, 3

Verdict:

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

correct output
5 5 

user output
(empty)

Test 11 (public)

Group: 2, 3

Verdict:

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

correct output
20 

user output
(empty)

Test 12 (public)

Group: 2, 3

Verdict:

input
INCREASE A
INCREASE A

INCREASE B
INCREASE B
...

correct output
42 

user output
(empty)

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
(empty)

Test 14 (public)

Group: 3

Verdict:

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

correct output
12 

user output
(empty)

Test 15 (public)

Group: 3

Verdict:

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

correct output
531441 

user output
(empty)

Test 16 (public)

Group: 3

Verdict:

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

correct output
1337 

user output
(empty)

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
(empty)

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
(empty)