day 6 complete

This commit is contained in:
2026-02-07 19:07:03 +00:00
parent 54d79ff7a6
commit 4f0471e040

View File

@@ -2,7 +2,7 @@
#include<stdlib.h> #include<stdlib.h>
#include<string.h> #include<string.h>
FILE *fptr; FILE *fptr;
int helper = 0;
struct __attribute__((packed)) { struct __attribute__((packed)) {
unsigned int w0 : 1; unsigned int w0 : 1;
unsigned int w1 : 1; unsigned int w1 : 1;
@@ -12,8 +12,19 @@ struct __attribute__((packed)) {
unsigned int f0 : 1; unsigned int f0 : 1;
} bitfield; } bitfield;
struct Information {
int turn_on;
int turn_off;
int toggle;
int x1;
int y1;
int x2;
int y2;
};
typedef struct lightarray { typedef struct lightarray {
int8_t **lights; int64_t **lights;
int rows_of_lights; int rows_of_lights;
int columns_of_lights; int columns_of_lights;
//int number·of·houses; //int number·of·houses;
@@ -27,6 +38,110 @@ typedef struct lightarray {
} __attribute__((packed)) Lightarray; } __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 //initial array allocation
Lightarray *initlightarray(){ Lightarray *initlightarray(){
Lightarray* f = (Lightarray*)malloc(sizeof(Lightarray)); Lightarray* f = (Lightarray*)malloc(sizeof(Lightarray));
@@ -35,87 +150,169 @@ Lightarray *initlightarray(){
f->rows_of_lights = 1000; f->rows_of_lights = 1000;
f->columns_of_lights = 1000; f->columns_of_lights = 1000;
f->lights = (int8_t**)malloc(f->rows_of_lights * sizeof(int8_t*));//allocating the rows f->lights = (int64_t**)malloc(f->rows_of_lights * sizeof(int64_t*));//allocating the rows
if (f->lights == NULL) { if (f->lights == NULL) {
free(f); free(f);
return NULL; //handling if there isn't enough memory to allocate or allocation fails return NULL; //handling if there isn't enough memory to allocate or allocation fails
} }
f->lights[0] = (int8_t*)malloc(f->columns_of_lights * sizeof(int8_t)); for (int i=0;i <= f->rows_of_lights; i++){
if (f->lights[0] == NULL){ f->lights[i] = (int64_t*)malloc(f->columns_of_lights * sizeof(int64_t));
free(f->lights); helper = i;
free(f); if (f->lights[helper] == NULL){
return NULL; for (int j = 0; j <= helper;j++){
} free(f->lights[j]);
}
free(f->lights);
free(f);
return NULL;
}}
return f; 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(){ int main(){
char compare_1[35]; char compare_1[35];
char compare_2[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_1;
int pos_2; 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"); fptr = fopen("input", "r");
if(fptr == NULL) { if(fptr == NULL) {
printf("Not able to open the file."); printf("Not able to open the file.");
return 1; return 1;
} }
Lightarray* my_light_array = initlightarray(0); Lightarray *my_light_array = initlightarray();
Lightarray *second_light_array = initlightarray();
if (my_light_array == NULL){ if (my_light_array == NULL){
fprintf(stderr, "ERROR:initialisation of struct failed\n"); fprintf(stderr, "ERROR:initialisation of struct failed\n");
return 1; return 1;
} }
if (second_light_array == NULL){
fprintf(stderr, "ERROR:initialisation of struct failed\n");
return 1;
}
char buffer[266]; char buffer[266];
while (fgets(buffer, sizeof(buffer), fptr)) { while (fgets(buffer, sizeof(buffer), fptr)) {
bitfield.w0 = 0; //print_light_array(my_light_array);
bitfield.w1 = 0; av = information_function(buffer);
bitfield.w2 = 0; my_light_array = update_light_array(my_light_array,av);
bitfield.w3 = 0; second_light_array = update_second_light_array(second_light_array,av);
bitfield.w4 = 0;
bitfield.f0 = 0;
sprintf(compare_1,"%.4s", buffer);
sprintf(compare_2,"%.3s", buffer + 5);
printf("%s\n\n",buffer);
printf("%s\n",compare_2);
if (strcmp(compare_1,"turn")){
bitfield.w0 = 1;
printf("turn found\n");
}else{
bitfield.w1 =1;
printf("toggle found\n");
}
if (bitfield.w0 == 1 && strcmp(compare_2,"on")){
bitfield.w2 = 1;
} else if (bitfield.w0 == 1 && strcmp(compare_2,"off")){
bitfield.w3 = 1;
}
//scrape section
pos_1 = strcspn(buffer,"123456789");
pos_2 = strcspn(buffer,",");
//pos_1 to pos_2 is the length of the first number
} }
//print_light_array(my_light_array);
get_array_total(my_light_array);
get_array_total_2(second_light_array);
} }