#include <stdlib.h>
#include <stdio.h>

int **trap_sweeper(char **mat, int rows, int cols){
    //Vraiment la merde les matrices
    int **mat_clc = malloc(rows * sizeof(int));
    if (mat_clc == NULL) { return NULL;}

    // index, x, temp
    // le y = rows actuel
    int *values = calloc(5,sizeof(int));
    if (values == NULL) { free(mat_clc); return NULL;}
    //0 -> x
    //1 -> y
    //2 -> xt
    //3 -> yt 
    //4 -> count
    
    *(values + 1) = 0;

    while (*(values + 1 ) < rows){

        *(mat_clc + *(values + 1)) = calloc(cols, sizeof(int));
        *values = 0;

        while(*values < cols){
            
            if ( *( *(mat + *(values + 1)) + *values) == '#'){
            
                *( *(mat_clc + *(values + 1)) + *values) = -1;

            }
            else {

                *(values + 4) = 0;
                *(values + 3) = *(values + 1) - 1;

                while (*(values + 3) <= *(values + 1) + 1){
                    
                    *(values + 2) = *values - 1;
                    while (*(values + 2) <= *values + 1){
                        
                        if(*(values + 3) >= 0 && *(values + 3) < rows && *(values + 2) >= 0 && *(values + 2) < cols) {
                    
                            if(*( *(mat + *(values + 3)) + *(values + 2)) == '#') (*(values + 4)) ++;
                        }
                        (*(values + 2)) ++;
                    }
                    (*(values + 3)) ++;
                }
                *(*(mat_clc + *(values + 1)) + *values) = *(values + 4);
            }
            (*values) ++;
        }
        (*(values + 1)) ++;
    }
    
    return mat_clc;

}
/*
int main()
{
    char *mat1[] = {
        "OOO#O",    // 1  1  2 -1  2
        "#OO#O",    //-1  3  4 -1  2
        "O##OO",    // 2 -1 -1  3  2
        "OOOO#"     // 1  2  2  2 -1
    };
    int **res = trap_sweeper(mat1, 4, 5);
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 5; ++j)
            printf("%d ", res[i][j]);
        printf("\n");
    }

    for (int i = 0; i < 4; ++i)
        free(res[i]);
    free(res);
}*/
