#include #include #include FILE *fptr; int helper = 0; struct __attribute__((packed)) { unsigned int w0 : 1; unsigned int w1 : 1; unsigned int w2 : 1; unsigned int w3 : 1; unsigned int w4 : 1; unsigned int f0 : 1; } bitfield; struct Information { int turn_on; int turn_off; int toggle; int x1; int y1; int x2; int y2; }; typedef struct lightarray { int64_t **lights; int rows_of_lights; int columns_of_lights; //int number·of·houses; //int x·coord; //int y·coord; //int x·coord·bot; //int y·coord·bot; //int x_coord_origin; //int y_coord_origin; //int seq·no; } __attribute__((packed)) Lightarray; struct Information information_function(char buffer[266]){ struct Information av; char buffer_2[266]; char buffer_3[266]; char buffer_4[266]; char buffer_5[266]; char buffer_6[266]; char buffer_7[266]; memset(buffer_2,'\0',sizeof(buffer_2)); memset(buffer_3,'\0',sizeof(buffer_3)); memset(buffer_4,'\0',sizeof(buffer_4)); memset(buffer_5,'\0',sizeof(buffer_5)); memset(buffer_6,'\0',sizeof(buffer_6)); memset(buffer_7,'\0',sizeof(buffer_7)); int pos_1 = 0; int pos_2 = 0; int pos_3 = 0; int pos_4 = 0; int pos_5 = 0; int pos_6 = 0; int pos_7 = 0; int pos_8 = 0; int pos_9 = 0; int pos_10 = 0; av.turn_off = 0; av.turn_on = 0; av.toggle = 0; av.x1 = 0; av.y1 = 0; av.x2 = 0; av.y2 = 0; const char s[2] = " "; const char s2[2] = ","; const char s3[2] = "\n"; const char s4[2] = "f"; const char s5[2] = "g"; const char s6[2] = "\0"; pos_8 = strcspn(buffer, s4); pos_9 = strcspn(buffer, s5); if (pos_9 < 6){ av.toggle = 1; //printf("\ntoggle true\n"); pos_1 = 7; } else if (pos_8 < 9){ av.turn_off =1; //printf("\noff true\n"); pos_1 = 9; } else { av.turn_on = 1; //printf("\non true\n"); pos_1 = 8; } //printf("\n%d on %d off %d toggle\n", av.turn_on, av.turn_off, av.toggle); strcpy(buffer_2, &buffer[pos_1]);//removes turn on, turn off or toggle pos_2 = strcspn(buffer_2, s2); //printf("%d pos_2\n",pos_2); strcpy(buffer_3, &buffer_2[pos_2+1]);//trims off x1 and the comma //printf("%s\n",buffer_3); strncpy(buffer_5,buffer_2,pos_2);//extracts x1 buffer_5[pos_2] ='\0'; //printf("%s buffer_5\n\n",buffer_5); //printf("%s first number %s buffer_2\n",buffer_5,buffer_2); av.x1 = atoi(buffer_5);//Good thus far //printf("%s\n",buffer_3); pos_3 = strcspn(buffer_3, s); strncpy(buffer_4,buffer_3,pos_3); buffer_4[pos_3] = '\0'; //printf("%s buffer 4\n\n\n",buffer_4); av.y1 = atoi(buffer_4);//Believe good to here pos_10 = strcspn(buffer_3, s5); strcpy(buffer_2,&buffer_3[pos_10 + 3]); //printf("%sbuffer 2\n",buffer_2); pos_4 = strcspn(buffer_2, s2); strncpy(buffer_6,buffer_2,pos_4); buffer_6[pos_4] = '\0'; //printf("%s buffer 6\n",buffer_6);//think good to here av.x2 = atoi(buffer_6); strcpy(buffer_4,&buffer_2[pos_4+1]); //printf("%sbuffer 4\n",buffer_4); pos_5 = strcspn(buffer_4, s3); strncpy(buffer_7,buffer_4,pos_5); buffer_7[pos_5] = '\0'; //printf("%s buffer 7\n",buffer_7); av.y2 = atoi(buffer_7); memset(buffer_2,'\0',sizeof(buffer_2)); memset(buffer_3,'\0',sizeof(buffer_3)); memset(buffer_4,'\0',sizeof(buffer_4)); memset(buffer_5,'\0',sizeof(buffer_5)); memset(buffer_6,'\0',sizeof(buffer_6)); memset(buffer_7,'\0',sizeof(buffer_7)); return av; } //initial array allocation Lightarray *initlightarray(){ Lightarray* f = (Lightarray*)malloc(sizeof(Lightarray)); if (f == NULL) return NULL; f->rows_of_lights = 1000; f->columns_of_lights = 1000; f->lights = (int64_t**)malloc(f->rows_of_lights * sizeof(int64_t*));//allocating the rows if (f->lights == NULL) { free(f); return NULL; //handling if there isn't enough memory to allocate or allocation fails } for (int i=0;i <= f->rows_of_lights; i++){ f->lights[i] = (int64_t*)malloc(f->columns_of_lights * sizeof(int64_t)); helper = i; if (f->lights[helper] == NULL){ for (int j = 0; j <= helper;j++){ free(f->lights[j]); } free(f->lights); free(f); return NULL; }} return f; } //AI Generated code here for debugging (google gemini) void print_light_array(Lightarray *array) { //for (int i = 0; i < array->rows_of_lights; i++) { //for (int j = 0; j < array->columns_of_lights; j++) { // Use %d (int promotion) //printf("%4d", array->lights[i][j]); //} //printf("\n"); // New line after each row //} printf("%4ld cell 999,999",array->lights[999][999]); } //End of AI void get_array_total(Lightarray *array) { int total = 0; printf("\n%d rows of lights\n",array->rows_of_lights); for (int i = 0; i <= array->rows_of_lights; i++) { for (int j = 0; j <= array->columns_of_lights; j++) { if (array->lights[i][j] == 1){ total = total + 1; } } } printf("total %d\n", total); } void get_array_total_2(Lightarray *array) { int total = 0; int var_2 = 0; for (int i = 0; i <= array->rows_of_lights; i++) { for (int j = 0; j <= array->columns_of_lights; j++) { var_2 = array->lights[i][j]; total = total + var_2; } } printf("total %d\n", total); } //struct Lightarray update_light_array(Lightarray *array, struct Information input) { Lightarray *update_light_array(Lightarray *array, struct Information input) { for (int i = input.x1; i <= input.x2; i++) { for (int j = input.y1; j <= input.y2; j++) { // Use %d (int promotion) if (input.turn_off == 1 && input.turn_on == 0 && input.toggle == 0){ if (array->lights[i][j] == 1){ array->lights[i][j] = 0; } else if (array->lights[i][j] == 0){ array->lights[i][j] = 0; } else { printf("error!!\n\n"); } } else if (input.turn_on == 1 && input.turn_off == 0 && input.toggle == 0){ if (array->lights[i][j] == 0){ array->lights[i][j] = 1; } else if (array->lights[i][j] == 1){ array->lights[i][j] = 1; } else { printf("error!!\n\n"); } } else if (input.toggle == 1 && input.turn_on == 0 && input.turn_off == 0){ if (array->lights[i][j] == 1){ array->lights[i][j] = 0; } else if (array->lights[i][j] ==0){ array->lights[i][j] =1; } else { printf("error!!\n\n"); } } //printf("%4d", array->lights[i][j]); } //printf("\n"); // New line after each row } return array; } Lightarray *update_second_light_array(Lightarray *array, struct Information input) { int var_3 = 0; for (int i = input.x1; i <= input.x2; i++) { for (int j = input.y1; j <= input.y2; j++) { var_3 = array->lights[i][j]; // Use %d (int promotion) if (input.turn_off == 1 && input.turn_on == 0 && input.toggle == 0 && var_3 >= 1){//reduce brightness by 1 var_3 = var_3 - 1; } else if (input.turn_on == 1 && input.turn_off == 0 && input.toggle == 0){//increase brightness by 1 var_3 = var_3 + 1; } else if (input.toggle == 1 && input.turn_on == 0 && input.turn_off == 0){//increase brightness by 2 var_3 = var_3 + 2; } //printf("%4d", array->lights[i][j]); array->lights[i][j] = var_3; } //printf("\n"); // New line after each row } return array; } int main(){ char compare_1[35]; char compare_2[35]; char compare_3[35]; char compare_4[35]; char compare_5[35]; char compare_6[35]; char buffer_2[35]; char buffer_3[35]; char buffer_4[35]; char buffer_5[35]; int pos_1; int pos_2, pos_3, pos_4, pos_5,pos_6,pos_7; int scratch_1, scratch_2, scratch_3, scratch_4,scratch_5; int x1,y1,x2,y2; struct Information av; fptr = fopen("input", "r"); if(fptr == NULL) { printf("Not able to open the file."); return 1; } Lightarray *my_light_array = initlightarray(); Lightarray *second_light_array = initlightarray(); if (my_light_array == NULL){ fprintf(stderr, "ERROR:initialisation of struct failed\n"); return 1; } if (second_light_array == NULL){ fprintf(stderr, "ERROR:initialisation of struct failed\n"); return 1; } char buffer[266]; while (fgets(buffer, sizeof(buffer), fptr)) { //print_light_array(my_light_array); av = information_function(buffer); my_light_array = update_light_array(my_light_array,av); second_light_array = update_second_light_array(second_light_array,av); } //print_light_array(my_light_array); get_array_total(my_light_array); get_array_total_2(second_light_array); }