From 12ec76316432e63692386ea0bcfb1988e01efc1c Mon Sep 17 00:00:00 2001 From: ThomasBallantine Date: Wed, 17 Sep 2025 23:15:12 +0100 Subject: [PATCH] Adding the completion of day 3 --- 2015_day_3/Makefile | 2 +- 2015_day_3/README.md | 24 ++++++ 2015_day_3/day_3.c | 197 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 186 insertions(+), 37 deletions(-) diff --git a/2015_day_3/Makefile b/2015_day_3/Makefile index f7ec8fb..950f9d6 100644 --- a/2015_day_3/Makefile +++ b/2015_day_3/Makefile @@ -1,5 +1,5 @@ compile: - gcc day_3.c -Wall -Werror=switch -o day_3 + gcc day_3.c -Wall -Werror=switch -pedantic -Wextra -o day_3 run: ./day_3 \ No newline at end of file diff --git a/2015_day_3/README.md b/2015_day_3/README.md index 5499b47..a540288 100644 --- a/2015_day_3/README.md +++ b/2015_day_3/README.md @@ -40,3 +40,27 @@ For example: ^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location. ^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses. + + +Your puzzle answer was 2565. + +The first half of this puzzle is complete! It provides one gold star: * +--- Part Two --- + +The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him. + +Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year. + +This year, how many houses receive at least one present? + +For example: + + ^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south. + ^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started. + ^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other. + + +PART 2 COMMENTS +I may have spoken too soon about liking having only one struct in play. I think that I'm going to stick with it though. Debate is as to whether or not to add extra fields to the first struct, or to create a new struct with different fields. I think that the move functions are basically sound at this point and ought to be able to be expanded to switch between 2 different cursors - this was fairly straightforwards. I debated adding support for an arbitrary number of cursors, but that seemed like overkill, although judicious struct modification would have enabled it + +Your puzzle answer was 2639. diff --git a/2015_day_3/day_3.c b/2015_day_3/day_3.c index e1b4bcb..5dda3b1 100644 --- a/2015_day_3/day_3.c +++ b/2015_day_3/day_3.c @@ -10,13 +10,15 @@ typedef struct housearray { 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; } Housearray; //initial array allocation -Housearray *inithousearray(void){ +Housearray *inithousearray(int bot){ Housearray* f = (Housearray*)malloc(sizeof(Housearray)); if (f == NULL) return NULL; @@ -27,6 +29,8 @@ Housearray *inithousearray(void){ f->number·of·houses = 0; f->x·coord = 0; f->y·coord = 0; + f->x·coord·bot =0; + f->y·coord·bot =0; f->seq·no = 0; f->houses = (int**)malloc(f->rows·in·array * sizeof(int*));//allocating the rows @@ -40,7 +44,11 @@ Housearray *inithousearray(void){ free(f); return NULL; } + if (bot == 1){ + f->houses[0][0] = 2;//Santa and Robo-Santa have been to the first house + } else{ f->houses[0][0] = 1;//santa has been to the starting point + } return f; } @@ -68,13 +76,23 @@ Housearray *inithousearray(void){ // //https://stackoverflow.com/questions/2151084/map-a-2d-array-onto-a-1d-array // //Think I might need to reexamine indexing //} -void movenorth(Housearray* f, char direction){ +void movenorth(Housearray* f, int user){ int old_rows = f->rows·in·array; int old_cols = f-> columns·in·array; + int north_y = 0; + int north_x = 0; //update y coordinate - f->y·coord--; + if (user == 1){ + f->y·coord·bot--; + north_y = f->y·coord·bot; + north_x = f->x·coord·bot; + } else{ + f->y·coord--; + north_y = f->y·coord; + north_x = f->x·coord; + } //check if expansion needed - if (f->y·coord - f->y_coord_origin < 0){ + if (north_y - f->y_coord_origin < 0){ //Expand north int new_rows = old_rows +1; int new_cols = old_cols; @@ -94,19 +112,30 @@ void movenorth(Housearray* f, char direction){ f->y_coord_origin--; } //updating the visit count - int grid_x = f->x·coord - f->x_coord_origin; - int grid_y = f->y·coord - f->y_coord_origin; + int grid_x = north_x - f->x_coord_origin; + int grid_y = north_y - f->y_coord_origin; f->houses[grid_y][grid_x]++; //comment } -void movesouth(Housearray* f, char direction){ +void movesouth(Housearray* f, int user){ int old_rows = f->rows·in·array; int old_cols = f-> columns·in·array; + int south_y = 0; + int south_x = 0; //update y coordinate - f->y·coord++; + if (user == 1){ + f->y·coord·bot++; + south_x = f->x·coord·bot; + south_y = f->y·coord·bot; + } else{ + f->y·coord++; + south_x = f->x·coord; + south_y = f->y·coord; + } + //check if expansion needed - if (f->y·coord - f->y_coord_origin >= f->rows·in·array){ + if (south_y - f->y_coord_origin >= f->rows·in·array){ //Expand south int new_rows = old_rows +1; int new_cols = old_cols; @@ -123,28 +152,42 @@ void movesouth(Housearray* f, char direction){ } //updating the visit count - int grid_x = f->x·coord - f->x_coord_origin; - int grid_y = f->y·coord - f->y_coord_origin; + int grid_x = south_x - f->x_coord_origin; + int grid_y = south_y - f->y_coord_origin; f->houses[grid_y][grid_x]++; } -void moveeast(Housearray* f, char direction){ +void moveeast(Housearray* f, int user){ int old_rows = f->rows·in·array; int old_cols = f-> columns·in·array; + int east_y = 0; + int east_x = 0; //update x coordinate - f->x·coord++; + //f->x·coord++; + if (user ==1){ + f->x·coord·bot++; + east_x = f->x·coord·bot; + east_y = f->y·coord·bot; + } else{ + f->x·coord++; + east_x = f->x·coord; + east_y = f->y·coord; + } //check if expansion needed - if (f->x·coord < 0){ + if (east_x - f->x_coord_origin >= f->columns·in·array){ //Expand east int new_rows = old_rows; int new_cols = old_cols + 1; //creates an array of pointers to point to my rows of integers - int** new_grid;// = (int**)malloc(new_rows * sizeof(int*)); + int** new_grid = (int**)malloc(new_rows * sizeof(int*)); for (int i = 0; i< old_rows; ++i){ int *new_row = malloc((new_cols) * sizeof(int));//allocating the new row - new_row[0] = 0;//initialising the leftmost element - memcpy(&new_row[1], f->houses[i], new_cols * sizeof(int));//copying the rest of the elements over + //new_row[0] = 0;//initialising the leftmost element + memcpy(&new_row[0], f->houses[i], old_cols * sizeof(int));//copying the rest of the elements over + new_row[old_cols] = 0;//Initially was lacking this line, because it wasn't set to zero, some extra values showed up there and increased the count of houses which recieved gifts + free(f->houses[i]); new_grid[i] = new_row; + //new_grid[i] = f->houses[i]; } free(f->houses); @@ -154,36 +197,52 @@ void moveeast(Housearray* f, char direction){ } //updating the visit count - int grid_x = f->x·coord - f->x_coord_origin; - int grid_y = f->y·coord - f->y_coord_origin; + int grid_x = east_x - f->x_coord_origin; + int grid_y = east_y - f->y_coord_origin; f->houses[grid_y][grid_x]++; } -void movewest(Housearray* f, char direction){ +void movewest(Housearray* f, int user){ int old_rows = f->rows·in·array; int old_cols = f-> columns·in·array; - //update y coordinate - f->y·coord++; + int west_y = 0; + int west_x =0; + //update x coordinate + //f->x·coord--; + if (user ==1){ + f->x·coord·bot--; + west_x = f->x·coord·bot; + west_y = f->y·coord·bot; + } else{ + f->x·coord--; + west_x = f->x·coord; + west_y = f->y·coord; + } //check if expansion needed - if (f->y·coord - f->y_coord_origin >= f->rows·in·array){ + if (west_x- f->x_coord_origin < 0){ //Expand south - int new_rows = old_rows +1; - int new_cols = old_cols; + int new_rows = old_rows; + int new_cols = old_cols +1; //creates an array of pointers to point to my rows of integers int** new_grid = (int**)malloc(new_rows * sizeof(int*)); for (int i = 0; i< old_rows; ++i){ - new_grid[i] = f->houses[i]; + int *new_row = malloc((new_cols) * sizeof(int));//allocating the new row + new_row[0] = 0;//initialising the leftmost element + memcpy(&new_row[1], f->houses[i], old_cols * sizeof(int));//copying the rest of the elements over + free(f->houses[i]); + new_grid[i] = new_row; + //new_grid[i] = f->houses[i]; } - new_grid[new_rows - 1] = (int*)calloc(new_cols, sizeof(int));//Initialising the new row to empty free(f->houses); f->houses = new_grid; f->rows·in·array = new_rows; f->columns·in·array = new_cols; + f->x_coord_origin--; } //updating the visit count - int grid_x = f->x·coord - f->x_coord_origin; - int grid_y = f->y·coord - f->y_coord_origin; + int grid_x = west_x - f->x_coord_origin; + int grid_y = west_y- f->y_coord_origin; f->houses[grid_y][grid_x]++; } @@ -191,7 +250,8 @@ void movewest(Housearray* f, char direction){ int main(){ - Housearray* my_house_array = inithousearray(); + int counter_r, counter_e =0; + Housearray* my_house_array = inithousearray(0); if (my_house_array == NULL){ fprintf(stderr, "ERROR:initialisation of struct failed\n"); return 1; @@ -202,7 +262,7 @@ int main(){ char left = '<'; char right = '>'; - char ch; + char ch, zh; fptr = fopen("2015_day_3_input.txt", "r"); if(fptr == NULL) { printf("Not able to open the file."); @@ -213,21 +273,86 @@ int main(){ printf("%c", ch); if (ch == up){ printf("\nUP"); - movenorth(my_house_array, ch); + movenorth(my_house_array, 0); }else if (ch == down){ printf("\nDOWN"); - movesouth(my_house_array, ch); + movesouth(my_house_array,0); }else if (ch == left){ - printf("\nEAST"); - moveeast(my_house_array, ch); + printf("\nWEST"); + movewest(my_house_array, 0); + }else if (ch == right){ + moveeast(my_house_array,0); } + ++my_house_array->seq·no; printf("\n%d ycoordinate\n",my_house_array->y·coord); printf("\n%d rows in array\n",my_house_array->rows·in·array); printf("\n%d cols in array\n",my_house_array->columns·in·array); -} + } + for (int i = 0; i < my_house_array->rows·in·array; ++i){ + for (int x = 0; x< my_house_array->columns·in·array; ++x){ + printf("%d",my_house_array->houses[i][x]); + if (my_house_array->houses[i][x] > 0){ + ++counter_r; + } + } + } + + printf("\n\n%d houses recieved at least one gift", counter_r); + printf("\nEND PART ONE\n"); + Housearray* robo_house_array = inithousearray(1); + if (robo_house_array == NULL){ + fprintf(stderr, "ERROR:initialisation of struct failed\n"); + return 1; + } + fptr = fopen("2015_day_3_input.txt", "r"); + if(fptr == NULL) { + printf("Not able to open the file."); + return 1; + } + while ((zh = fgetc(fptr)) != EOF) {//every other char goes to robo-santa + printf("\n%c", zh); + if(robo_house_array->seq·no % 2 == 0){ + if (zh == up){ + printf("\nUP"); + movenorth(robo_house_array, 0); + }else if (zh == down){ + printf("\nDOWN"); + movesouth(robo_house_array,0); + }else if (zh == left){ + printf("\nWEST"); + movewest(robo_house_array, 0); + }else if (zh == right){ + moveeast(robo_house_array,0); + } + } else{ + if (zh == up){ + printf("\nUP"); + movenorth(robo_house_array, 1); + }else if (zh == down){ + printf("\nDOWN"); + movesouth(robo_house_array,1); + }else if (zh == left){ + printf("\nWEST"); + movewest(robo_house_array, 1); + }else if (zh == right){ + moveeast(robo_house_array,1); + } + } + ++robo_house_array->seq·no; + } + for (int i = 0; i < robo_house_array->rows·in·array; ++i){ + for (int x = 0; x< robo_house_array->columns·in·array; ++x){ + printf("%d",robo_house_array->houses[i][x]); + if (robo_house_array->houses[i][x] > 0){ + ++counter_e; + } + } + } + + printf("\n\n%d houses recieved at least one gift", counter_e); } \ No newline at end of file