Advent of code 2025 (not proud)
1#include <stddef.h> 2#include <stdio.h> 3#include <stdlib.h> 4 5 6int 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} 7 int neighbors = -1; 8 int i_0 = 0; 9 int i_last = 0; 10 int j_0= 0; 11 int j_last = 0; 12 13 if (i == 0) { 14i_0 = 1; 15 } else if (last_i) { 16 17i_last = 1; 18 } 19 if (j==0) { 20 21j_0 = 1; 22 } else if (last_j) { 23 24j_last = 1; 25 } 26 27 // 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. 28 // while counting it for the neighbors it will still result in true for %2. 29 // once a run is over, we iterate over the whole matrix again and reduce all 3's by 1. 30 for (int ii = -1+i_0; ii<=1-i_last; ii++) { 31 for (int jj = -1+j_0; jj<=1-j_last ; jj++) { 32 if (matrix[i+ii][j+jj]%2) { 33 neighbors++; 34 } 35 } 36 } 37 if (neighbors<4) { 38 return 1; 39 } else { 40 return 0; 41 } 42} 43 44int main(int argc, char* argv[]) { 45 46 47 FILE* f = NULL; 48 char* line = NULL; 49 size_t size = 0; 50 ssize_t nread = 0; 51 int len = 0; 52 int w = 0; 53 int access = 0; 54 int accesible_rolls = 0; 55 int total_accesible_rolls = 0; 56 57 int** matrix_ptr = NULL; 58 59 60 f = fopen(argv[1], "r"); 61 62 63 while ((nread = getline(&line, &size, f)) != -1) { 64 w = nread-2; 65 matrix_ptr = (int **)realloc(matrix_ptr, (len+1) * (nread-2)*sizeof(int)); 66 matrix_ptr[len] = (int *)malloc((nread-2)*sizeof(int)); 67 for (int i = 0; i< nread-1; i++) { 68 if (line[i] == '@') { 69 matrix_ptr[len][i] = 1; 70 71 } else { 72 matrix_ptr[len][i] = 0; 73 } 74 // printf("%d",(&matrix_ptr)[j][i]); 75 } 76 len++; 77 } 78 79 // just a matrix out of input. 80 81 // 82 // [(i-1,j-1)][(i-1,j)] [(i-1,j+1)] 83 // [(i,j-1)] [(i,j)] [(i,j+1)] 84 // [(i+1,j-1)][(i+1,j)] [(i+1,j+1)] 85 // check all of those in clockwise order 86 // 87 88 do { 89accesible_rolls=0; 90 for (int i = 0; i<len; i++) { 91 for (int j = 0; j<w+1; j++) { 92 if(matrix_ptr[i][j]==1) { 93 access = get_acces(matrix_ptr, i, j, (i==len-1), (j==w)); 94 if (access) { 95 matrix_ptr[i][j]+=2; 96 accesible_rolls++; 97 printf("x"); 98 }else { 99 100 printf("@"); 101 } 102 } else { 103 printf("."); 104 } 105 106 } 107 printf("\n"); 108 } 109 110 111 for (int i = 0; i<len; i++) { 112 for (int j = 0; j<w+1; j++) { 113 if(matrix_ptr[i][j]==3) { 114 matrix_ptr[i][j]--; 115 116 } 117 }} 118 119 printf("\n\n accesible rolls: %d\n",accesible_rolls); 120 total_accesible_rolls+=accesible_rolls; 121 }while (accesible_rolls>0); 122 123 124 125 126 127 printf("\n\n total accesible rolls: %d\n",total_accesible_rolls); 128 129 130 free(matrix_ptr); 131 free(f); 132 free(line); 133 return 0; 134}