#include "vision.h"
#include <bits/stdc++.h>

/**
    tova sus spodeleniq desktop beshe mega brainrot
    * "chat tova real li e"
    * "papka Desktop/Racheva folder"
    * drugi, koito bqha tolkova necenzurni, che daje nqma da gi pisha tuka
    * "IATI SOLUTIONS", v koqto imashe "lapai.txt"
*/

using namespace std;

vector<int> getVisionPattern1d(){
    return {1, 1, 2, 1, 2, 2};
}

int getMove1d(vector<int> v){
    int mid = (v.size()-1)/2;
    if(v[mid] == 1){
        if(v[mid+1] == 2 && v[mid-1] == 1){
            return mid+1;
        }else if(v[mid+1] == 1 && v[mid-1] == 2){
            return mid-1;
        }else if(v[mid+1] == 2 && v[mid-1] == 2){
            return mid+1; /// vse taq
        }
    }else if(v[mid] == 2){
        int leftNum = v[mid-1]*10 + v[mid-2];
        int rightNum = v[mid+1]*10 + v[mid+2];
        if((leftNum == 11 && rightNum == 12) || (leftNum == 12 && rightNum == 11)){
            return rightNum==12 ? mid+2 : mid-2;
        }else if((leftNum == 12 && rightNum == 21) || (leftNum == 21 && rightNum == 12)){
            return rightNum==21 ? mid+2 : mid-2;
        }else if((leftNum == 21 && rightNum == 11) || (leftNum == 11 && rightNum == 21)){
            return rightNum==11 ? mid+2 : mid-2;
        }
    }
    return 0; /// da ne mrunka
}

vector<vector<int>> getVisionPattern2d(){
    return {
        {1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 2},
        {1, 2, 1, 1, 1, 1},
        {1, 1, 1, 1, 2, 1},
    };
}

pair<int, int> getMove2d(vector<vector<int>> v){
    int mr = (v.size()-1)/2, mc = (v[0].size()-1)/2;
    if(v[mr][mc] == 1){
        int nxtR = 1, nxtC = 1;
        int mx = -1;
        for(int i = -1; i <= +1; i++){
            for(int j = -1; j <= +1; j++){
                if(i == 0 && j == 0){ continue; }
                if(v[mr+i][mc+j] >= mx){
                    nxtR = mr+i; nxtC = mc+j;
                    mx = v[nxtR][nxtC];
                }
            }
        }
        if(mx == 1){
            return {mr+1, mc};
        }
        return {nxtR, nxtC};
    }else if(v[mr][mc] == 2){
        vector<pair<int, int>> neighOff;
        for(int i = -2; i <= +2; i++){
            for(int j = -2; j <= +2; j++){
                if(i == 0 && j == 0){ continue; }
                if(v[mr+i][mc+j] == 2){ neighOff.push_back({i, j}); } /// tozi if go bqh zabravil, FAAAAH
            }
        }
        if(neighOff.size() == 1){
            return {mr - neighOff[0].first, mc - neighOff[0].second};
        }else if(neighOff.size() == 2){
            return {mr + neighOff[0].first, mc + neighOff[0].second}; // vse taq
        }else if(neighOff.size() == 3){
            for(int i = 0; i < 3; i++){ cerr << "(" << neighOff[i].first << " " << neighOff[i].second << "), "; } cerr << '\n';
            int idx = -1;
            if(neighOff[0].second == neighOff[1].second){ idx = 2; }
            else if(neighOff[0].second == neighOff[2].second){ idx = 1; }
            else if(neighOff[1].second == neighOff[2].second){ idx = 0; }
            if(idx == -1){exit(0);}
            return {mr + neighOff[idx].first, mc + neighOff[idx].second};
        }
        return {neighOff.size(), neighOff.size()};
    }
    return {0, 0}; /// da ne mrunka
}
