#include "vision.h"
#include <iostream>
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 {
        {2, 1, 3},
        {1, 1, 1},
        {4, 1, 5}
    };
}

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

    if (v[idy][idx + 1] == 3)
        return {idy + 1, idx + 1};
    else if (v[idy][idx + 1] == 5)
        return {idy, idx + 1};
    else if (v[idy][idx + 1] == 1) {
        if (v[idy - 1][idx] == 1)
            return {idy + 1, idx + 1};
        else if (v[idy + 1][idx] == 5)
            return {idy + 1, idx};
        else if (v[idy + 1][idx] == 4)
            return {idy + 1, idx};
    } else if (v[idy][idx] == 5) {
        int vertdir = 0;
        int hordir = 0;
        if (v[idy - 2][idx] == 3)
            vertdir = 1;
        else
            vertdir = -1;
        if (v[idy][idx - 2] == 4)
            hordir = 1;
        else
            hordir = -1;
        return {
            vertdir == 1 ? v.size() - 1 : 0,
            hordir == 1 ? v[0].size() - 1 : 0
        };
    } else if (v[idy][idx] == 4) {
        int vertdir = 0;
        int hordir = 0;
        if (v[idy - 2][idx] == 2)
            vertdir = 1;
        else
            vertdir = -1;
        if (v[idy][idx + 2] == 5)
            hordir = 1;
        else
            hordir = -1;
        return {
            vertdir == 1 ? v.size() - 1 : 0,
            hordir == 1 ? v[0].size() - 1 : 0
        };
    } else if (v[idy][idx] == 3) {
        int vertdir = 0;
        int hordir = 0;
        if (v[idy + 2][idx] == 5)
            vertdir = 1;
        else
            vertdir = -1;
        if (v[idy][idx - 2] == 2)
            hordir = 1;
        else
            hordir = -1;
        return {
            vertdir == 1 ? v.size() - 1 : 0,
            hordir == 1 ? v[0].size() - 1 : 0
        };
    } else if (v[idy][idx] == 2) {
        int vertdir = 0;
        int hordir = 0;
        if (v[idy + 2][idx] == 4)
            vertdir = 1;
        else
            vertdir = -1;
        if (v[idy][idx + 2] == 3)
            hordir = 1;
        else
            hordir = -1;
        return {
            vertdir == 1 ? v.size() - 1 : 0,
            hordir == 1 ? v[0].size() - 1 : 0
        };
    }
    return ans;
}
