#include "vision.h"
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

#define sz(x) ((int)((x).size())) 

int dx[] = {-1, 1, 1, -1};
int dy[] = {1, 1, -1, -1};

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

int getMove1d(vector<int> v)
{
    if (sz(v) == 7) {
		if (v[1] == 2) return 6;
		return 0;
	}
	int best = 0, maxi = 0;
	for (int i = 0; i < sz(v); i++) {
		if (v[i] > maxi) {
			maxi = v[i];
			best = i;
		}
	}
	return best;
}

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

pair<int, int> getMaxi(const vector<vector<int>> &v)
{
	int maxi = 0;
	pair<int, int> best = {0, 0};
	for (int i = 0; i < sz(v); i++) {
		for (int j = 0; j < sz(v); j++) {
			if (2*i+1 == sz(v) && 2*j+1 == sz(v)) continue;
			if (v[i][j] > maxi) {
				maxi = v[i][j];
				best = {i, j};
			}
		}
	}
	return best;
}

pair<int, int> getMaxiNoCorners(const vector<vector<int>> &v)
{
	int maxi = 0;
	pair<int, int> best = {0, 0};
	for (int i = 0; i < sz(v); i++) {
		for (int j = 0; j < sz(v); j++) {
			if (2*i+1 == sz(v) && 2*j+1 == sz(v)) continue;
			if ((i == 0 || i == sz(v)-1) && (j == 0 || j == sz(v)-1)) continue;
			if (v[i][j] > maxi) {
				maxi = v[i][j];
				best = {i, j};
			}
		}
	}
	return best;
}



std::pair<int, int> getMove2d(std::vector<std::vector<int>> v)
{
	int k = sz(v)/2;
    if (k == 3) {
		for (int dir = 0; dir < 4; dir++) {
			if (v[3+2*dx[dir]][3+2*dy[dir]] == 1) {
				return {3+3*dx[dir], 3+3*dy[dir]};
			}
		}
	}
	if (k == 2) {
		auto [i, j] = getMaxi(v);
		if (v[i][j] == 3) {
			return {i, j};
		}
		return getMaxiNoCorners(v);
	}
	return getMaxi(v);
}
