//chockolateman

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

using namespace std;

std::vector<int> getVisionPattern1d()
{
    vector<int> ret;
    ret.push_back(2);
    ret.push_back(1);
    ret.push_back(1);
    ret.push_back(3);
    ret.push_back(1);
    return ret;
}

int nxt(int val)
{
    if(val != 3)
        return val+1;
    return 1;
}

int getMove1d(std::vector<int> v)
{
    int K = v.size();
    int mid = K/2;
    int val = v[mid];
    if(val==1)
    {
        if((v[mid-1]==2 && v[mid+1]==3) || (v[mid-1]==3 && v[mid+1]==2))
        {
            for(int i = 0 ; i < K ; i++)
                if(v[i]==2)
                    return i;
        } 
        else if((v[mid-1]==1 && v[mid+1]==3) || (v[mid-1]==3 && v[mid+1]==1))
        {
            for(int i = 0 ; i < K ; i++)
                if(v[i]==3)
                    return i;
        } 
        else if((v[mid-1]==1 && v[mid+1]==2) || (v[mid-1]==2 && v[mid+1]==1))
        {   
            if(v[mid-1]==1)
                return mid-1;
            else
                return mid+1;
        } 
    }
    else if(val==2)
    {
        if(v[mid-2]==3)
            return mid+1;
        else
            return mid-1;
    }
    else
    {
        for(int i = mid-2 ; i <= mid+2 ; i++)
            if(v[i]==2)
                return i;
    }
}

std::vector<std::vector<int>> getVisionPattern2d()
{
    vector<vector<int>> ret;
    vector<int> temp;
    temp.push_back(2);
    temp.push_back(1);
    temp.push_back(1);
    temp.push_back(3);
    temp.push_back(1);
    ret.push_back(temp); //maybe I will need to swap rows and columns later
    temp.clear();
    temp.push_back(2);
    temp.push_back(2);
    temp.push_back(2);
    temp.push_back(2);
    temp.push_back(2);
    ret.push_back(temp);
    temp.clear();
    temp.push_back(1);
    temp.push_back(1);
    temp.push_back(1);
    temp.push_back(1);
    temp.push_back(1);
    ret.push_back(temp);
    return ret;
}

bool is_special(vector<vector<int>> v)
{
    int K = v[0].size();
    int mid = K/2;
    int val = v[mid][mid];
    for(int j = mid ; j <= mid+1 ; j++)
        if(v[mid][j] != v[mid][j-1])
            return false;
    return true;
}

std::pair<int, int> getMove2d(std::vector<std::vector<int>> v)
{
    int K = v[0].size();
    int mid = K/2;
    int val = v[mid][mid];
    if(is_special(v))
    {
        if(val==1)
        {
            if(v[mid-1][mid]==2)
                return {mid+1,mid};
            else
                return {mid-1,mid};
        }
        else
        {
            if(v[mid+1][mid]==1)
                return {mid+1,mid};
            else
                return {mid-1,mid};
        }
    }
    else
    {
        vector<int> temp;
        for(int j = 0 ; j < K ; j++)
            temp.push_back(v[mid][j]);
        int new_j = getMove1d(temp);
        if(v[mid+1][mid]==2)
            return {mid+1,new_j};
        else
            return {mid-1,new_j};
    }
}