#include #include #include int get_acces(int** matrix,int i,int j,int last_i,int last_j) { // last_{i,j} are bools to indicate we are in the last row} int neighbors = -1; int i_0 = 0; int i_last = 0; int j_0= 0; int j_last = 0; if (i == 0) { i_0 = 1; } else if (last_i) { i_last = 1; } if (j==0) { j_0 = 1; } else if (last_j) { j_last = 1; } // for step 2 we will be working modulus 2. during the run, once we establish a roll as accesible it will get replaces with 3. // while counting it for the neighbors it will still result in true for %2. // once a run is over, we iterate over the whole matrix again and reduce all 3's by 1. for (int ii = -1+i_0; ii<=1-i_last; ii++) { for (int jj = -1+j_0; jj<=1-j_last ; jj++) { if (matrix[i+ii][j+jj]%2) { neighbors++; } } } if (neighbors<4) { return 1; } else { return 0; } } int main(int argc, char* argv[]) { FILE* f = NULL; char* line = NULL; size_t size = 0; ssize_t nread = 0; int len = 0; int w = 0; int access = 0; int accesible_rolls = 0; int total_accesible_rolls = 0; int** matrix_ptr = NULL; f = fopen(argv[1], "r"); while ((nread = getline(&line, &size, f)) != -1) { w = nread-2; matrix_ptr = (int **)realloc(matrix_ptr, (len+1) * (nread-2)*sizeof(int)); matrix_ptr[len] = (int *)malloc((nread-2)*sizeof(int)); for (int i = 0; i< nread-1; i++) { if (line[i] == '@') { matrix_ptr[len][i] = 1; } else { matrix_ptr[len][i] = 0; } // printf("%d",(&matrix_ptr)[j][i]); } len++; } // just a matrix out of input. // // [(i-1,j-1)][(i-1,j)] [(i-1,j+1)] // [(i,j-1)] [(i,j)] [(i,j+1)] // [(i+1,j-1)][(i+1,j)] [(i+1,j+1)] // check all of those in clockwise order // do { accesible_rolls=0; for (int i = 0; i0); printf("\n\n total accesible rolls: %d\n",total_accesible_rolls); free(matrix_ptr); free(f); free(line); return 0; }