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

std::vector<int> getVisionPattern1d() {
    return {1, 1, 2, 3};
}
int getMove1d(std::vector<int> v) {
    int mid = v.size() / 2;
    if (v[mid] == 2) {
        if (v[mid + 1] == 3) return mid + 1;
        else return mid - 1;
    }
    if (v[mid] == 3) {
        if (v[mid + 1] == 1) return mid + 1;
        else return mid - 1;
    }
    if (v[mid] == 1) {
        if (v[mid + 1] == 2) return mid + 1;
        if (v[mid - 1] == 2) return mid - 1;
        if (v[mid + 1] == 3) return mid - 1;
        if (v[mid - 1] == 3) return mid + 1;
    }
    return -1;
}
std::vector<std::vector<int>> getVisionPattern2d() {
    return {
        {1, 2, 1}, 
        {2, 3, 1},
        {1, 4, 1}    
    };
}
std::vector<std::pair<int, int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
std::pair<int, int> getMove2d(std::vector<std::vector<int>> v) {
    int mid = v.size() / 2;
    if (v[mid][mid] == 1) {
        std::map<int, int> cnt;
        for (auto [dx, dy] : dir) {
            cnt[v[mid + dx][mid + dy]]++;
        }
        if (cnt[2] == 0) {
            for (auto [dx, dy] : dir) {
                if (v[mid + dx][mid + dy] == 4) return {mid - dx, mid - dy};
            }
        }
        if (cnt[2] == 1) {
            if (cnt[3] == 1) {
                for (auto [dx, dy] : dir) {
                    if (v[mid + dx][mid + dy] == 3) return {mid - dx, mid - dy};
                }
            } else {
                for (auto [dx, dy] : dir) {
                    if (v[mid + dx][mid + dy] == 2) return {mid - dx, mid - dy};
                }
            }
        }
        if (cnt[2] == 2) {
            for (auto [dx, dy] : dir) {
                if (v[mid + dx][mid + dy] == 2) return {mid + dx, mid + dy};
            }
        }
    }
    if (v[mid][mid] == 2) {
        for (auto [dx, dy] : dir) {
            if (v[mid + dx][mid + dy] == 3) return {mid + dx, mid + dy};
        }
    }
    if (v[mid][mid] == 3) {
        for (auto [dx, dy] : dir) {
            if (v[mid + dx][mid + dy] == 1) return {mid + dx, mid + dy};
        }
    }
    if (v[mid][mid] == 4) {
        for (auto [dx, dy] : dir) {
            if (v[mid + dx][mid + dy] == 2) return {mid + dx, mid + dy};
        }
    }
    return {-1, -1};
}