#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() {
    return {{1, 2, 2}, {1, 1, 1}, {1, 1, 2}};
}

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] + 
            v[vision + 1][vision - 1] + 
            v[vision - 1][vision + 1] + 
            v[vision + 1][vision + 1] == 7
            ) {
            if (v[vision - 1][vision - 1] == 1) return {vision - 1, vision - 1};
            if (v[vision + 1][vision - 1] == 1) return {vision + 1, vision - 1};
            if (v[vision - 1][vision + 1] == 1) return {vision - 1, vision + 1};
            if (v[vision + 1][vision + 1] == 1) return {vision + 1, vision + 1};
        }

        int sum = 0;

        for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
                sum += v[vision + x][vision + y];
            }
        }

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

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

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

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

    return {};
}
