#include <iostream>
#include "vision.h"
using namespace std;

void shift(vector <int>& v) {
    v.push_back(v.front());
    v.erase(v.begin());
}

vector <int> operator<<(vector <int> v, int b) {
    while (b--) {
        shift(v);
    }
    
    return v;
}

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

int getMove1d(std::vector<int> v) {
    int vision = (int) v.size() / 2;

    if (v[vision] == 1) {
        if (v[vision - 1] != 1) return vision - 1;
        return vision + 1;
    }

    if (v[vision] == 2) {
        int mx = 0;
    
        for (int i = 0; i < v.size(); i++) {
            if (i == vision) continue;
            mx = max(mx, v[i]);
        }
    
        for (int i = 0; i < v.size(); i++) {
            if (i == vision) continue;
            if (mx == v[i]) return i;
        }
    }

    if (v[vision + 2] == 2) return vision - 3;
    return vision + 3;
}

std::vector<std::vector<int>> getVisionPattern2d() {
    vector <vector <int>> v(16, vector <int> (16, 1));

    v[0][0] = 2;
    v[2][2] = 2;
    v[4][4] = 2;
    v[6][6] = 4;
    v[8][8] = 2;
    v[10][10] = 4;
    v[12][12] = 2;
    v[14][14] = 6;

    return v;
}

std::pair<int, int> getMove2d(std::vector<std::vector<int>> v) {
    int vision = (int) v.size() / 2;

    if (v[vision][vision] == 1) {
        if (v[vision - 1][vision] != 1) return {vision - 1, vision};
        if (v[vision + 1][vision] != 1) return {vision + 1, vision};
        if (v[vision][vision - 1] != 1) return {vision, vision - 1};
        return {vision, vision + 1};
    }

    if (v[vision][vision] == 2) {
        if (v[vision - 2][vision - 2] != 2) return {vision - 2, vision - 2};
        return {vision + 2, vision + 2};
    }

    if (v[vision][vision] == 4) {
        int mx = 0;
    
        for (int i = 0; i < v.size(); i++) {
            if (i == vision) continue;
            mx = max(mx, v[i][i]);
        }
    
        for (int i = 0; i < v.size(); i++) {
            if (i == vision) continue;
            if (mx == v[i][i]) return {i, i};
        }
    }

    if (v[vision + 2][vision + 2] == 4) return {vision - 3, vision - 3};
    return {vision + 3, vision + 3};
}