#include "vision.h"
#include <algorithm>
#include <string>
std::vector<int> getVisionPattern1d()
{
  return {1, 1, 1, 2, 1, 3};
}

int getMove1d(std::vector<int> v)
{
  if(v.size() == 3) {
    for(int i = 0; i < v.size(); ++i) if(v[i] != 1) return i;
    return 0;
  }
  if(v.size() == 5) {
    if(v.front() == 3) return 0;
    return v.size()-1;
  }
  if(v[1] == 2) return v.size()-1;
  return 0;
}

std::vector<std::vector<int>> getVisionPattern2d()
{
  return 
  {{1, 1, 1, 1, 1, 1, 1, 1},
  {1, 2, 1, 1, 2, 1, 2, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 2, 1, 1, 2, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 2, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 2, 1, 1, 2, 1},
  {1, 1, 1, 1, 1, 1, 1, 1}};
}
std::pair<int, int> rotate(std::pair<int, int> p, int r) {
  if(r == 0) return p;
  if(r == 1) return {p.first, 4-p.second};
  if(r == 2) return {4-p.first, p.second};
  return {4-p.first, 4-p.second};
}
std::pair<int, int> getMove2d(std::vector<std::vector<int>> v){
  std::vector<std::pair<std::string, std::pair<int, int>>> poses;
  poses.push_back({"44", {2, 4}});
  poses.push_back({"244144", {2, 4}});
  poses.push_back({"2042", {4, 2}});
  poses.push_back({"000340", {0, 2}});
  poses.push_back({"0002", {4, 4}});
  poses.push_back({"0434", {3, 4}});
  poses.push_back({"10", {4, 3}});
  poses.push_back({"", {4, 4}});
  if(v.size() == 3) {
    for(int i = 0; i < 3; ++i) for(int j = 0; j < 3; ++j) if(v[i][j] != 1) return {i, j};
    return {2, 2};
  }
  std::vector<std::pair<int, int>> pos;
  for(int i = 0; i < 5; ++i) for(int j = 0; j < 5; ++j) if(v[i][j] != 1 && (i != 2 || j != 2)) pos.push_back({i, j});
  for(int i = 0; i < 4; ++i) {
    std::vector<std::pair<int, int>> np;
    for(auto p : pos) np.push_back(rotate(p, i));
    std::sort(np.begin(), np.end());
    std::string s;
    for(auto p : np) {
      s += std::to_string(p.first);
      s += std::to_string(p.second);
    }
    for(auto pp : poses) if(pp.first == s) return rotate(pp.second, i);
  }
  return {4, 4};
}
