Submission details
Task:Tulkki
Sender:jokeri2222
Submission time:2025-11-04 14:27:30 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:89:5: error: 'function' was not declared in this scope
   89 |     function<int(int, const string&, int)> REPEAT;
      |     ^~~~~~~~
input/code.cpp:7:1: note: 'std::function' is defined in header '<functional>'; did you forget to '#include <functional>'?
    6 | #include <algorithm>
  +++ |+#include <functional>
    7 | 
input/code.cpp:89:41: error: expression list treated as compound expression in functional cast [-fpermissive]
   89 |     function<int(int, const string&, int)> REPEAT;
      |                                         ^
input/code.cpp:89:14: error: expected primary-expression before 'int'
   89 |     function<int(int, const string&, int)> REPEAT;
      |              ^~~
input/code.cpp:90:5: error: 'REPEAT' was not declared in this scope
   90 |     REPEAT = [&](int index_of_code, const string& var_times, int _loop_idx) -> int {
      |     ^~~~~~

Code

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <sstream>
#include <algorithm>

using namespace std;

int nth_index(const vector<string>& l, const string& item, int n) {
    int count = 0;
    for (int idx = 0; idx < (int)l.size(); ++idx) {
        if (l[idx] == item) {
            count++;
            if (count == n) return idx;
        }
    }
    return -1; // not found
}

int main() {
    vector<string> program;
    vector<string> commands = {
        "PRINT",
        "CLEAR",
        "INCREASE",
        "REPEAT"
    };

    // Read input lines up to 1000 or until break
    for (int i = 0; i < 1000; ++i) {
        string _input;
        if (!getline(cin, _input)) break;
        // Remove "TIMES"
        size_t pos;
        while ((pos = _input.find("TIMES")) != string::npos) {
            _input.erase(pos, 5);
        }
        if (_input == "\\r") break;

        // Split by '#' and take first part
        pos = _input.find('#');
        string before_comment = (pos == string::npos) ? _input : _input.substr(0, pos);

        // Split by spaces and filter empty
        istringstream iss(before_comment);
        string token;
        vector<string> sub_prog;
        while (iss >> token) {
            if (!token.empty()) sub_prog.push_back(token);
        }

        if (!sub_prog.empty()) {
            program.insert(program.end(), sub_prog.begin(), sub_prog.end());
        }
    }

    vector<string> stack;
    int counter = 0;
    for (int i = 0; i < (int)program.size(); ++i) {
        if (program[i] == "(") {
            stack.push_back("(" + to_string(counter));
            counter++;
        }
        if (program[i] == ")") {
            int loop_start_idx = nth_index(program, "(", (int)stack.size());
            string loop_start = stack.back();
            stack.pop_back();
            program[loop_start_idx] = loop_start;
            program[i] = ")" + loop_start.substr(1);
        }
    }

    vector<int> output;
    unordered_map<string, int> VARS;

    auto CLEAR = [&](int, const string& var, int) -> int {
        VARS[var] = 0;
        return 0;
    };
    auto INCREASE = [&](int, const string& var, int) -> int {
        VARS[var] += 1;
        return 0;
    };
    auto PRINT = [&](int, const string& var, int) -> int {
        output.push_back(VARS[var]);
        return 0;
    };
    function<int(int, const string&, int)> REPEAT;
    REPEAT = [&](int index_of_code, const string& var_times, int _loop_idx) -> int {
        string _command_used = "";
        int _end_idx = -1;
        for (int i = 0; i < (int)program.size(); ++i) {
            if (program[i] == (")" + to_string(_loop_idx))) {
                _end_idx = i;
                break;
            }
        }
        if (_end_idx == -1) return index_of_code;

        for (int _ = 0; _ < VARS[var_times]; ++_) {
            int _next_idx = index_of_code;
            for (int _i = index_of_code; _i <= _end_idx; ++_i) {
                if (_i < _next_idx) continue;
                const string& _sub = program[_i];
                if (!_command_used.empty()) {
                    if (VARS.find(_sub) == VARS.end()) {
                        VARS[_sub] = 0;
                    }
                    int next_loop_idx = _loop_idx;
                    if (_command_used == "REPEAT") {
                        if (_i + 1 <= _end_idx) {
                            string next_loop_str = program[_i + 1];
                            if (!next_loop_str.empty() && next_loop_str[0] == ')') {
                                next_loop_idx = stoi(next_loop_str.substr(1));
                            }
                        }
                    }
                    if (_command_used == "CLEAR") {
                        CLEAR(_i + 1, _sub, next_loop_idx);
                    } else if (_command_used == "INCREASE") {
                        INCREASE(_i + 1, _sub, next_loop_idx);
                    } else if (_command_used == "PRINT") {
                        PRINT(_i + 1, _sub, next_loop_idx);
                    } else if (_command_used == "REPEAT") {
                        _next_idx = REPEAT(_i + 1, _sub, next_loop_idx);
                    }
                    _command_used = "";
                }
                if (find(commands.begin(), commands.end(), _sub) != commands.end()) {
                    _command_used = _sub;
                }
            }
        }
        return _end_idx + 1;
    };

    string command_used = "";
    int next_idx = -1;
    int loop_idx = 0;
    for (int i = 0; i < (int)program.size(); ++i) {
        if (i < next_idx) continue;
        const string& sub = program[i];
        if (!command_used.empty()) {
            if (VARS.find(sub) == VARS.end()) {
                VARS[sub] = 0;
            }
            if (command_used == "REPEAT") {
                if (i + 1 < (int)program.size()) {
                    string next_loop_str = program[i + 1];
                    if (!next_loop_str.empty() && next_loop_str[0] == ')') {
                        loop_idx = stoi(next_loop_str.substr(1));
                    }
                }
            }
            if (command_used == "CLEAR") {
                CLEAR(i + 1, sub, loop_idx);
                next_idx = i + 1;
            } else if (command_used == "INCREASE") {
                INCREASE(i + 1, sub, loop_idx);
                next_idx = i + 1;
            } else if (command_used == "PRINT") {
                PRINT(i + 1, sub, loop_idx);
                next_idx = i + 1;
            } else if (command_used == "REPEAT") {
                next_idx = REPEAT(i + 1, sub, loop_idx);
            }
            command_used = "";
        }
        if (find(commands.begin(), commands.end(), sub) != commands.end()) {
            command_used = sub;
        }
    }

    for (size_t i = 0; i < output.size(); ++i) {
        if (i > 0) cout << " ";
        cout << output[i];
    }
    cout << "\n";

    return 0;
}