piscine/r01/ex00/solver.c
Willem Haffmans 607ce08c18 all
2024-09-10 00:18:01 +02:00

99 lines
2.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* solver.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/16 12:44:08 by whaffman #+# #+# */
/* Updated: 2024/06/16 13:52:11 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int **make_the_array(void);
int exists_in_array(int *row);
void display_grid(int **grid);
int find_candidate(int front_view, int reverse_view, int index);
void add_to_grid(int **grid, int solution_id, int row)
{
int column;
int **the_array;
column = 0;
the_array = make_the_array();
while (column < 4)
{
grid[row][column] = the_array[solution_id][column + 2];
column++;
}
free(the_array);
}
void remove_from_grid(int **grid, int row)
{
int column;
column = 0;
while (column < 4)
{
grid[row][column] = 0;
column++;
}
}
int check_grid(int *cond, int **grid)
{
int column_index;
int column[6];
int row;
column_index = 0;
while (column_index < 4)
{
column[0] = cond[column_index];
column[1] = cond[column_index + 4];
row = 0;
while (row < 4)
{
column[2 + row] = grid[row][column_index];
row++;
}
if (!exists_in_array(&column[0]))
return (0);
column_index++;
}
return (1);
}
int solve(int *cond, int **grid, int row)
{
int f_view;
int r_view;
int i;
int solved;
solved = 0;
if (row == 4 && check_grid(cond, grid))
{
display_grid(grid);
return (1);
}
else
{
f_view = cond[8 + row];
r_view = cond[12 + row];
i = 0;
while (find_candidate(f_view, r_view, i) >= 0 && !solved)
{
add_to_grid(grid, find_candidate(f_view, r_view, i), row);
solved = solve(cond, grid, row + 1);
remove_from_grid(grid, row);
i++;
}
return (solved);
}
}