138 lines
2.9 KiB
C
138 lines
2.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* main.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/07/23 18:04:54 by kjimenez #+# #+# */
|
|
/* Updated: 2022/07/24 13:58:55 by kjimenez ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <stdlib.h>
|
|
#include "checks.h"
|
|
#include "parser.h"
|
|
#include "permutations.h"
|
|
#include "lib.h"
|
|
|
|
void print_board(int **board, int count)
|
|
{
|
|
int i;
|
|
int j;
|
|
|
|
if (count == 4)
|
|
{
|
|
i = -1;
|
|
while (++i < 4)
|
|
{
|
|
j = -1;
|
|
while (++j < 4)
|
|
{
|
|
ft_putchar(board[i][j] + '0');
|
|
if (j < 3)
|
|
ft_putchar(' ');
|
|
}
|
|
ft_putchar('\n');
|
|
}
|
|
exit(0);
|
|
}
|
|
free(board);
|
|
}
|
|
|
|
int **create_board(int *permut_1, int *permut_2, int *permut_3, int *permut_4)
|
|
{
|
|
int **board;
|
|
int i;
|
|
|
|
board = (int **) malloc(4 * sizeof(int *));
|
|
i = -1;
|
|
while (++i < 4)
|
|
board[i] = (int *) malloc(4 * sizeof(int));
|
|
i = -1;
|
|
while (++i < 4)
|
|
board[0][i] = permut_1[i];
|
|
i = -1;
|
|
while (++i < 4)
|
|
board[1][i] = permut_2[i];
|
|
i = -1;
|
|
while (++i < 4)
|
|
board[2][i] = permut_3[i];
|
|
i = -1;
|
|
while (++i < 4)
|
|
board[3][i] = permut_4[i];
|
|
return (board);
|
|
}
|
|
|
|
void solve_board(int **board, int **arr_expected)
|
|
{
|
|
int i;
|
|
int count;
|
|
|
|
i = 0;
|
|
count = 0;
|
|
if (check_duplicates(board))
|
|
{
|
|
while (i < 4)
|
|
{
|
|
if (up_to_down(board, i, arr_expected[0][i])
|
|
&& down_to_up(board, i, arr_expected[1][i])
|
|
&& left_to_right(board, i, arr_expected[2][i])
|
|
&& right_to_left(board, i, arr_expected[3][i]))
|
|
count++;
|
|
i++;
|
|
}
|
|
}
|
|
print_board(board, count);
|
|
}
|
|
|
|
void solve_all_boards(int **permutations, int **arr_expected)
|
|
{
|
|
int line_1;
|
|
int line_2;
|
|
int line_3;
|
|
int line_4;
|
|
|
|
line_1 = -1;
|
|
while (++line_1 < 24)
|
|
{
|
|
line_2 = -1;
|
|
while (++line_2 < 24)
|
|
{
|
|
line_3 = -1;
|
|
while (++line_3 < 24)
|
|
{
|
|
line_4 = -1;
|
|
while (++line_4 < 24)
|
|
{
|
|
solve_board(create_board(permutations[line_1],
|
|
permutations[line_2], permutations[line_3],
|
|
permutations[line_4]), arr_expected);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ft_putstr("Error\n");
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int **permutations;
|
|
int **check;
|
|
|
|
if (argc == 2)
|
|
{
|
|
if (check_input(argv[1]))
|
|
{
|
|
check = str_to_tab(argv[1]);
|
|
permutations = generate_permutations();
|
|
solve_all_boards(permutations, check);
|
|
free(permutations);
|
|
free(check);
|
|
return (0);
|
|
}
|
|
}
|
|
ft_putstr("Error\n");
|
|
return (0);
|
|
}
|