#include "triangles.h"

#include <vector>
using namespace std;

bool isIn(Triangle t, int h) {
    Triangle t3 = {t.i, t.j, t.k};
    Triangle t1 = {t.i, t.k, h};
    Triangle t0 = {t.j, t.k, h};
    Triangle t2 = {t.i, t.j, h};
    if (compare({t1, t2, t0}, {t3}) == 0) {
        return true;
    }
    return false;
}

bool isConv(Triangle t, int i) {
    Triangle t1 = {t.i, t.k, i};
    Triangle t2 = {t.j, t.k, i};
    Triangle t3 = {t.i, t.j, i};
    if (compare({t, t1}, {t2, t3}) == 0) {
        return true;
    }
    return false;
}

std::vector<std::vector<int>> solve(int n) {
    vector<int> hull = {0, 1, 2};
    for (int h = 3; h < n; h++) {
        vector<int> used(n);
        for (int l = 0; l < hull.size(); l++) {
            if (hull.size() == 2) {
                break;
            }
            Triangle t = {hull[(l + hull.size() - 1) % hull.size()], h, hull[(l + 1) % hull.size()]};
            if (isIn(t, hull[l])) {
                used[l] = -1;
                return {{t.i, t.j, t.k}};
            }
        }
        if (isIn({0, 1, 2}, h)) {
            return {{0, 1, 2}};
        }
        Triangle t3 = {0, 1, 2};
        Triangle t1 = {0, 2, 3};
        Triangle t0 = {1, 2, 3};
        Triangle t2 = {0, 1, 3};
        if (compare({t1, t2, t3}, {t0}) == 0) {
            return {{0, 1, 2}, {0, 2, 3}, {0, 1, 3}};
        }
        if (compare({t0, t2, t3}, {t1}) == 0) {
            return {{0, 1, 2}, {0, 1, 3}, {2, 1, 3}};
        }
        if (compare({t1, t0, t3}, {t2}) == 0) {
            return {{0, 1, 2}, {0, 2, 3}, {2, 1, 3}};
        }
        if (compare({t3, t0}, {{t1, t2}}) == 0) {
            return {{0, 1, 2}, {1, 2, 3}};
        }
        if (compare({t2, t3}, {t1, t0}) == 0) {
            return {{0, 1, 3}, {0, 1, 2}};
        }
        if (compare({t1, t3}, {t2, t0}) == 0) {
            return {{0, 1, 3}, {2, 1, 3}};
        }
    }
    vector<vector<int> > ans;
    for (int i = 2; i < hull.size(); i++) {
        ans.push_back({hull[0], hull[i - 1], hull[i]});
    }
    return ans;
}
