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

using namespace std;

#define for0(i, n) for (ll i = 0; i < n; i++)
#define for1(i, n) for (ll i = 1; i < n; i++)
#define rfor0(i, n) for (ll i = n - 1; i >= 0; i--)
#define be(x) x.begin()
#define en(x) x.end()
#define all(x) be(x), en(x)
#define sz(x) x.size()
#define pb push_back
#define con continue
#define ret return

typedef long long ll;
typedef vector<ll> vll;

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

int getMove1d(std::vector<int> v) {
    if (v.size() == 3) {
        int mx = max(v[0], v[2]);
        if (mx == 3) {
            if (v[0] == 3) {
                return 0;
            } else {
                return 2;
            }
        } else if (mx == 2) {
            if (v[0] == 2) {
                return 0;
            } else {
                return 2;
            }
        } else {
            return 0;
        }
    } else if (v.size() == 5) {
        if (v[0] == 3) {
            return 0;
        } else {
            return 4;
        }
    } else {
        if (v[1] == 2) {
            return 6;
        } else {
            return 0;
        }
    }
}

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

void xot(vector<std::vector<int>>& v, vector<vector<pair<int, int>>>& p) {
    for (int i = 0; i < v.size() / 2; i++) {
        for (int j = 0; j < v.size(); j++) {
            swap(v[i][j], v[v.size() - 1 - i][j]);
            swap(p[i][j], p[v.size() - 1 - i][j]);
        }
    }
}

void yot(vector<std::vector<int>>& v, vector<vector<pair<int, int>>>& p) {
    for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v.size() / 2; j++) {
            swap(v[i][j], v[i][v.size() - 1 - j]);
            swap(p[i][j], p[i][v.size() - 1 - j]);
        }
    }
}

std::pair<int, int> getMove2d(std::vector<std::vector<int>> v) {
    vector<vector<pair<int, int>>> p(v.size(), vector<pair<int, int>>(v.size()));
    for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v.size(); j++) {
            p[i][j] = {i, j};
        }
    }
    if (v.size() == 3) {
        pair<int, int> mx = {0, 0};
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (v[i][j] > v[mx.first][mx.second]) {
                    mx = {i, j};
                }
            }
        }
        return mx;
    } else if (v.size() == 5) {
        int mn = 100;
        pair<int, int> mx;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (v[i][j] == 3) {
                    mx = {i, j};
                    mn = abs(2 - i) + abs(2 - j);
                }
            }
        }
        if (mn == 2) {
            if (mx.first == 0) {
                xot(v, p);
                mx.first = 4;
            }
            if (v[2][0] == 1) {
                yot(v, p);
            }
            return p[2][4];
        } else if (mn == 4) {
            if (mx.first == 0) {
                xot(v, p);
            }
            if (mx.second == 0) {
                yot(v, p);
            }
            return p[4][4];
        } else {
            return mx;
        }
    } else {
        if (v[1][3] == 1) {
            xot(v, p);
        }
        return p[6][3];
    }
}
