#include <bits/stdc++.h>
#include "vision.h"

using namespace std;

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

int getMove1d(std::vector<int> v)
{
    int sz = v.size();
    if(sz == 3) {
        int mx = 0;
        for(int i = 0; i < sz; i++) {
            mx = max(mx, v[i]);
        }
        for(int i = 0; i < sz; i++) {
            if(v[i] == mx) return i;
        }
    }
    if(sz == 5) {
        int c2 = 0;
        for(auto it : v) c2 += (it == 2);
        if(c2 == 3) {
            if(v[sz / 2 + 1] == 2) {
                return sz / 2 + 1;
            }
            return sz / 2 - 1;
        }
        if(max(v[sz / 2 + 1], v[sz / 2 - 1]) == 2) {
            if(v[sz / 2 + 1] == 2) {
                return 0;
            }
            return 4;
        }
        if(v[0] == 2) {
            return 0;
        }
        return 4;
    }
    if(v[0] == 1) {
        return 0;
    }
    return 6;
}

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

std::pair<int, int> getMove2d(std::vector<std::vector<int>> v)
{
    if(v.size() == 3) {
        int mx = 0;
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                mx = max(mx, v[i][j]);
            }
        }
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                if(v[i][j] == mx) {
                    return {i, j};
                }
            }
        }
        while(true);
    }
    if(v.size() == 5) {
        pair<int, int> ans;
        int dist = -67;
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5; j++) {
                if(v[i][j] != 3) continue;
                int d = abs(2 - i) + abs(2 - j);
                if(d > dist) {
                    ans = {i, j};
                    dist = d;
                }
            }
        }
        return ans;
    }
    if(v.size() == 7) {
        pair<int, int> ans;
        int dist = 67;
        for(int i = 0; i < 7; i++) {
            for(int j = 0; j < 7; j++) {
                if(v[i][j] != 2) continue;
                int d = abs(2 - i) + abs(2 - j);
                if(d < dist) {
                    ans = {i, j};
                    dist = d;
                }
            }
        }
        return ans;
    }
    while(true);
}
