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

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

int getMove1d(std::vector<int> v)
{
    int tot = v.size();
    int mid = tot / 2;
    int val = v[mid];
    if (val == 1) {
        if (v[mid-1] == 2) return mid-1;
        else return mid+1;
    }
    else if (val == 2) {
        if (v[mid+1] == 1 && v[mid-1] == 1) {
            if (v[mid+2] == 2) return mid+2;
            else {
                assert(v[mid-2] == 2);
                return mid-2;
            }
        }
        else if (v[mid-2] == 2) {
            return mid+1;
        }
        else if (v[mid+2] == 2) {
            return mid-1;
        }
        else if (v[mid-1] == 2) return mid+2;
        else return mid-2;
    }
    assert(false);
    return -1;
}

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

vector<pair<int, int>> delta = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
vector<pair<int, int>> delta2 = {{-2, -2}, {-2, 0}, {-2, 2}, {0, -2}, {0, 2}, {2, -2}, {2, 0}, {2, 2}};
vector<pair<int, int>> delta3 = {{-2, -2}, {-2, 2}, {2, -2}, {2, 2}};

std::pair<int, int> getMove2d(std::vector<std::vector<int>> v)
{
    int tot = v.size();
    int mid = tot / 2;
    int r = mid, c = mid;
    if (v[mid][mid] == 1) {
        for (auto [pr, pc]: delta) {
            if (v[pr + r][pc + c] > 1) return {pr + r, pc + c};
            assert(false);
        }
    }
    else if (v[mid][mid] == 2) {
        for (auto [pr, pc]: delta2) {
            if (v[pr + r][pc + c] == 3) return {pr + r, pc + c};
        }
    }
    else if (v[mid][mid] == 3) {
        for (int i = 0; i < 4; i++) {
            int pr = delta3[i].first;
            int pc = delta3[i].second;
            if (v[pr + r][pc + c] == 2) {
                if (i == 0) return {mid+2, mid+2};
                else if (i == 1) return {mid+2, mid-2};
                else if (i == 2) return {mid-2, mid+2};
                else if (i == 3) return {mid-2, mid-2};
            }
        }
    }
    assert(false);
    return {0, 0};
}
