Click Here to go back to the homepage.

GlitchBot Solution:


#include <bits/stdc++.h>
using namespace std;

pair<int, int> run(const string instructions[], const int length){
    int x = 0; int y = 0;
    int direction = 0;
    for(int i = 0; i < length; i++){
        if(instructions[i] == "Forward"){
            switch(direction){
                case 0:
                    y++;
                    break;
                case 1:
                    x++;
                    break;
                case 2:
                    y--;
                    break;
                case 3:
                    x--;
                    break;
                default:
                    break;
            }
        } else if(instructions[i] == "Right"){
            if(++direction > 3){
                direction = 0;
            }
        } else {
            if(--direction < 0){
                direction = 3;
            }
        }
    }
    return {x, y};
}

int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    int x, y, nInstructions;
    cin >> x >> y >> nInstructions;
    string instructions[nInstructions];
    for(int i = 0; i < nInstructions; i++){
        cin >> instructions[i];
    }
    for(int i = 0; i < nInstructions; i++){
        string original = instructions[i];
        for(int j = 0; j < 2; j++){
            if(instructions[i] == "Forward"){
                instructions[i] = "Right";
            } else if(instructions[i] == "Right"){
                instructions[i] = "Left";
            } else {
                instructions[i] = "Forward";
            }
            pair<int, int> result = run(instructions, nInstructions);
            if(result.first == x && result.second == y){
                cout << i + 1 << " " << instructions[i];
                goto end;
            }
        }
        instructions[i] = original;
    }
    end:
    return 0;   
}