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

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

int getMove1d(std::vector<int> v)
{
    int idx = v.size() / 2;
    if (v[idx] == 3) {
        if (v[0] == 2)
            return v.size() - 1;
        else
            return 0;
    } else if (v[idx] == 2) {
        return idx + 1;
    } else if (v[idx] == 1) {
        if (v[idx - 1] == 2)
            return idx + 1;
        else if (v[idx + 1] == 2)
            return idx - 1;
        else if (v[idx - 1] == 3)
            return idx - 1;
        else
            return idx + 1;
    }
}

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

std::pair<int, int> getMove2d(std::vector<std::vector<int>> v)
{   
    int idy = v.size() / 2;
    int idx = v[0].size() / 2;
    pair <int, int> ans = {idy, idx};

    [&]()->void {
        set <int> s = {
            v[idy][idx - 1],
            v[idy][idx],
            v[idy][idx + 1]
        };
        if (s.size() == 2) {
            for (int i = idx - 1; i <= idx + 1; i++)
                if (v[idy][i] == 2) {
                    ans.second = i;
                    return;
                }
        } else {
            if (v[idy][idx - 1] + 1 == v[idy][idx] || v[idy][idx] + 1 == v[idy][idx + 1])
                ans.second = idx + 1;
            else
                ans.second = idx - 1;
        }
    }();

    [&]()->void {
        set <int> s = {
            v[idy - 1][idx],
            v[idy][idx],
            v[idy + 1][idx]
        };
        if (s.size() == 2) {
            for (int i = idy - 1; i <= idy + 1; i++)
                if (v[i][idx] == 2) {
                    ans.first = i;
                    return;
                }
        } else {
            if (v[idy - 1][idx] + 1 == v[idy][idx] || v[idy][idx] + 1 == v[idy + 1][idx])
                ans.first = idy + 1;
            else
                ans.first = idy - 1;
        }
    }();

    return ans;
}
