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

using namespace std;

vector<vector<int>> solve(int n) {
    for (int a = 0; a < n; ++a) {
        for (int b = 0; b < n; ++b) {
            if (a == b) continue;
            for (int c = 0; c < n; ++c) {
                if (a == c || b == c) continue;
                for (int d = 0; d < n; ++d) {
                    if (a == d || b == d || c == d) continue;

                    vector<Triangle> left = {Triangle(a, b, c), Triangle(c, b, d)};
                    vector<Triangle> right = {Triangle(a, b, d), Triangle(c, a, d)};
                    
                    int val = compare(left, right);
                    if (val == 0) {
                        vector<vector<int>> ans;

                        ans.push_back({a, b, c});
                        ans.push_back({b, c, d});

                        return ans;
                    }
                }
            }
        }
    }

    vector<array<int, 3>> st;
    
    vector<int> p(4);
    iota(p.begin(), p.end(), 0);

    do {
        st.push_back({p[0], p[1], p[2]});
    } while (next_permutation(p.begin(), p.end()));

    for (int a = 0; a < st.size(); ++a) {
        for (int b = 0; b < st.size(); ++b) {
            int same_1 = 0;
            for (int k = 0; k < 3; ++k) {
                for (int l = 0; l < 3; ++l) {
                    if(st[a][k] == st[b][l]) {
                        same_1++;
                    }
                }
            }

            for (int c = 0; c < st.size(); ++c) {
                int same_2 = 0;
                for (int k = 0; k < 3; ++k) {
                    for (int l = 0; l < 3; ++l) {
                        if(st[a][k] == st[c][l]) {
                            same_2++;
                        }
                    }
                }

                int same_3 = 0;
                for (int k = 0; k < 3; ++k) {
                    for (int l = 0; l < 3; ++l) {
                        if(st[b][k] == st[c][l]) {
                            same_3++;
                        }
                    }
                }

                if (same_1 == same_2 && same_2 == same_3 && same_3 == 2) {
                    vector<Triangle> left;
                    
                    left.push_back(Triangle{st[a][0], st[a][1], st[a][2]});
                    left.push_back(Triangle{st[b][0], st[b][1], st[b][2]});
                    left.push_back(Triangle{st[c][0], st[c][1], st[c][2]});
                    
                    for (int d = 0; d < st.size(); ++d) {
                        vector<Triangle> right = {Triangle{st[d][0], st[d][1], st[d][2]}};
                        
                        int val = compare(left, right);
                        if (val == 0) {
                            vector<vector<int>> ans;

                            ans.push_back({st[a][0], st[a][1], st[a][2]});
                            ans.push_back({st[b][0], st[b][1], st[b][2]});
                            ans.push_back({st[c][0], st[c][1], st[c][2]});

                            return ans;
                        }
                    }
                }
            }
        }
    }

    exit(-1);
}
