From aff6562cfeb95686db5894bdd280b8b8e2b2489d Mon Sep 17 00:00:00 2001 From: ThomasBallantine Date: Tue, 12 Aug 2025 21:59:57 +0100 Subject: [PATCH] push --- 2015_day_3/2015_day_3_input.txt | 1 + 2015_day_3/day_3.c | 167 ++++++++++++++++++++++++++++++++ 2015_day_3/erronius code | 167 ++++++++++++++++++++++++++++++++ 3 files changed, 335 insertions(+) create mode 100644 2015_day_3/2015_day_3_input.txt create mode 100644 2015_day_3/day_3.c create mode 100644 2015_day_3/erronius code diff --git a/2015_day_3/2015_day_3_input.txt b/2015_day_3/2015_day_3_input.txt new file mode 100644 index 0000000..6e8d40e --- /dev/null +++ b/2015_day_3/2015_day_3_input.txt @@ -0,0 +1 @@ +^^<<>^^>^^^><^>v^>v><><><^^v>v^v>>>^<>v<^<^>>>>><>^>>^>v^>><<^>v>v<>^v^v^vvv><>^^>v><>^><^^^v>>^v^>v><>v^^><vv^<<>v>>><<<>>^^^vv>>>^><<<>><><^>v<>^>v<^v^><<<<>^<>v>^v>vv<^<<>>>>^^v>vv^^<>^<>^^^^<^^^vv<^^v^^>v>^v^^^^>><v<>^v^><>^^><<^^<^^>vv<>v^<^v^>^^>^<>v^^vv<>>v><<<>vvv<>v<>><^<^v<>^vv>^^v<^<>>vv<^>>^>>vv^v>^v^<>^>>>>vv>^^>v>vv>v><^vv^<^<<^^vv^^v>^>>v><^<>v<><>^^<>v>><>^^>^^>>vvv^><<<<<^<^vv<^<>^^^<<<^>^^^vv<>^<>v<^v>^<<>^v<>>v<<^<^<<<><><>^>>>>^>v^v<>vv<^vvv^^^^vv>^v^^v^<^vv<^vv>v<^>vv<>>^>^><^>v>^v>vvv<>^>^v<><>vv>><^v^<><>>v^v^><^<^>vv>v<^>vvv>v<<<<<^>^vv>^><><>^<v^>^><><>>^>^>><^^^>^^>^^v^^<^v^^>v^^>>><<><><>^^<<^^v^>v>><>^^^><^vvv<^^^^^v><<><><>>^>vv>>^vv^^><v<^^>^<^^<^>>>^v<>v<^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^>><>^^<>^v^^vv<><^>vv^^v^<^^^v^vvv^>><>>v<>^<^vvv<<^^>vv^^<<>>><^^vvv<<<^>^<><^>vv^><^<<>vv<>vv>v>v^<<>^^^^v^^^^^<<^><><^^v^>v>^>><^><<>v^>>^vvv>>^<^<>^^v^vv^^v><>>v<<<>v>^<>v<<>v^>^<<><<>v^>v<><^^>^<^v^^><^>vv>^>vvvv>^^><<>vv^>^v<<^<<^<<>vv>>^>>>>>v^v<^v>v>^^^vv^v<^<>v><>>vv>v><>v>^v<><<<<<>v^vv<<<<^<>^>><>^^vv>^<^<<>vv>>vv><>><^^><^<>^><>v^^^^v^^vv<>v<>v>^vv^>^<>^^^>v^>>>v><<^>>v<^v<>^^v<><<>v<^<^>v<>v^>v>^^<<<^^vv^<><<<>>v>^^<>v>>>><<>v^v<>v>><<<<^<<^>^>v^vv^><^v^^<>^^><>vv>^>vvv<^v^>>^>^>^^<<^>^>^v><>>^<^^v>^>>^^<><>>>^^>^^vvv>v<^^<>v^v^^v^v>><<^^^>>v>^vv>^>^^v<>^^<>v^^<>v^><<>vv<<^vvvv><<v>v^>v^<>v^>^^<>^>^^v<>><<<>^v^^v^v<<<^v^<>^<>v>^^>vv>^^<<<><<^>v<^^<^<<>^>>>>>^v^v<vvv<<>v>v>>^v^v^>><<<<>v^<<>>>^>>^>>< \ No newline at end of file diff --git a/2015_day_3/day_3.c b/2015_day_3/day_3.c new file mode 100644 index 0000000..741b98e --- /dev/null +++ b/2015_day_3/day_3.c @@ -0,0 +1,167 @@ +#include +#include +#include +FILE *fptr; + +typedef struct housearray { + int **houses; + int rows·in·array; + int columns·in·array; + int number·of·houses; + int x·coord; + int y·coord; + int x_coord_origin; + int y_coord_origin; + int seq·no; +} Housearray; + +//initial array allocation +Housearray *inithousearray(void){ + Housearray* f = (Housearray*)malloc(sizeof(Housearray)); + if (f == NULL) return NULL; + + f->rows·in·array =1; + f->columns·in·array = 1; + f->x_coord_origin = 0; + f->y_coord_origin =0; + f->number·of·houses = 0; + f->x·coord = 0; + f->y·coord = 0; + f->seq·no = 0; + + f->houses = (int**)malloc(f->rows·in·array * sizeof(int*));//allocating the rows + if (f->houses == NULL) { + free(f); + return NULL; //handling if there isn't enough memory to allocate or allocation fails + } + f->houses[0] = (int*)malloc(f->columns·in·array * sizeof(int)); + if (f->houses[0] == NULL){ + free(f->houses); + free(f); + return NULL; + } + f->houses[0][0] = 1;//santa has been to the starting point + return f; +} + + + + +//void move·santa(Housearray *f, char dir){ +// //check if array is empty +// int p2x·coord, p2y·coord = 0; +// if ( f->number·of·houses == 0) { +// f->number·of·houses = 9;//initialise length 9 +// f->rows·in·array = f->columns·in·array = 3;//initialise a 3x3 map +// f->x·coord = f->y·coord = 2;//Initialise to 2,2 +// else if (dir == "V"){ +// p2y·coord = f->y·coord -1; +// if (p2y·coord ==0){ +// +// } +// } +// +//} + +//void get·seq·no(int xcoord, int ycoord, int nocolumns, int norows, int numberofhouses){ +// //transforms coordinates into a sequence number +// //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){ + int old_rows = f->rows·in·array; + int old_cols = f-> columns·in·array; + int old_original_x = f->x_coord_origin; + int old_original_y = f->y_coord_origin; + //update y coordinate + f->y·coord--; + //check if expansion needed + if (f->y·coord < 0){ + //Expand north + int new_rows = old_rows +1; + int new_cols = old_cols; + //creates am 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+1] = f->houses[i];//repeat without the plus one for the southward add row + } + new_grid[0] = (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->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; + f->houses[grid_y][grid_x]++; +} + +void movesouth(Housearray* f, char direction){ + int old_rows = f->rows·in·array; + int old_cols = f-> columns·in·array; + int old_original_x = f->x_coord_origin; + int old_original_y = f->y_coord_origin; + //update y coordinate + f->y·coord++; + //check if expansion needed + if (f->y·coord > f->rows·in·array){ + //Expand south + int new_rows = old_rows +1; + int new_cols = old_cols; + //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]; + } + new_grid[0] = (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->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; + f->houses[grid_y][grid_x]++; +} + + +int main(){ + Housearray* my_house_array = inithousearray(); + if (my_house_array == NULL){ + fprintf(stderr, "ERROR:initialisation of struct failed\n"); + return 1; + } + char c; + char up = '^'; + char down = 'v'; + char left = '<'; + char right = '>'; + + char ch; + fptr = fopen("2015_day_3_input.txt", "r"); + if(fptr == NULL) { + printf("Not able to open the file."); + return 1; + } + //https://www.geeksforgeeks.org/c-program-to-read-contents-of-whole-file/ + while ((ch = fgetc(fptr)) != EOF) { + printf("%c", ch); + if (ch == up){ + printf("\nUP"); + movenorth(my_house_array, ch); + }else if (ch == down){ + printf("\nDOWN"); + movesouth(my_house_array, ch); + } + printf("\n%d ycoordinate\n",my_house_array->y·coord); + printf("\n%d rows in array\n",my_house_array->rows·in·array); + + +} + + +} \ No newline at end of file diff --git a/2015_day_3/erronius code b/2015_day_3/erronius code new file mode 100644 index 0000000..741b98e --- /dev/null +++ b/2015_day_3/erronius code @@ -0,0 +1,167 @@ +#include +#include +#include +FILE *fptr; + +typedef struct housearray { + int **houses; + int rows·in·array; + int columns·in·array; + int number·of·houses; + int x·coord; + int y·coord; + int x_coord_origin; + int y_coord_origin; + int seq·no; +} Housearray; + +//initial array allocation +Housearray *inithousearray(void){ + Housearray* f = (Housearray*)malloc(sizeof(Housearray)); + if (f == NULL) return NULL; + + f->rows·in·array =1; + f->columns·in·array = 1; + f->x_coord_origin = 0; + f->y_coord_origin =0; + f->number·of·houses = 0; + f->x·coord = 0; + f->y·coord = 0; + f->seq·no = 0; + + f->houses = (int**)malloc(f->rows·in·array * sizeof(int*));//allocating the rows + if (f->houses == NULL) { + free(f); + return NULL; //handling if there isn't enough memory to allocate or allocation fails + } + f->houses[0] = (int*)malloc(f->columns·in·array * sizeof(int)); + if (f->houses[0] == NULL){ + free(f->houses); + free(f); + return NULL; + } + f->houses[0][0] = 1;//santa has been to the starting point + return f; +} + + + + +//void move·santa(Housearray *f, char dir){ +// //check if array is empty +// int p2x·coord, p2y·coord = 0; +// if ( f->number·of·houses == 0) { +// f->number·of·houses = 9;//initialise length 9 +// f->rows·in·array = f->columns·in·array = 3;//initialise a 3x3 map +// f->x·coord = f->y·coord = 2;//Initialise to 2,2 +// else if (dir == "V"){ +// p2y·coord = f->y·coord -1; +// if (p2y·coord ==0){ +// +// } +// } +// +//} + +//void get·seq·no(int xcoord, int ycoord, int nocolumns, int norows, int numberofhouses){ +// //transforms coordinates into a sequence number +// //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){ + int old_rows = f->rows·in·array; + int old_cols = f-> columns·in·array; + int old_original_x = f->x_coord_origin; + int old_original_y = f->y_coord_origin; + //update y coordinate + f->y·coord--; + //check if expansion needed + if (f->y·coord < 0){ + //Expand north + int new_rows = old_rows +1; + int new_cols = old_cols; + //creates am 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+1] = f->houses[i];//repeat without the plus one for the southward add row + } + new_grid[0] = (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->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; + f->houses[grid_y][grid_x]++; +} + +void movesouth(Housearray* f, char direction){ + int old_rows = f->rows·in·array; + int old_cols = f-> columns·in·array; + int old_original_x = f->x_coord_origin; + int old_original_y = f->y_coord_origin; + //update y coordinate + f->y·coord++; + //check if expansion needed + if (f->y·coord > f->rows·in·array){ + //Expand south + int new_rows = old_rows +1; + int new_cols = old_cols; + //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]; + } + new_grid[0] = (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->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; + f->houses[grid_y][grid_x]++; +} + + +int main(){ + Housearray* my_house_array = inithousearray(); + if (my_house_array == NULL){ + fprintf(stderr, "ERROR:initialisation of struct failed\n"); + return 1; + } + char c; + char up = '^'; + char down = 'v'; + char left = '<'; + char right = '>'; + + char ch; + fptr = fopen("2015_day_3_input.txt", "r"); + if(fptr == NULL) { + printf("Not able to open the file."); + return 1; + } + //https://www.geeksforgeeks.org/c-program-to-read-contents-of-whole-file/ + while ((ch = fgetc(fptr)) != EOF) { + printf("%c", ch); + if (ch == up){ + printf("\nUP"); + movenorth(my_house_array, ch); + }else if (ch == down){ + printf("\nDOWN"); + movesouth(my_house_array, ch); + } + printf("\n%d ycoordinate\n",my_house_array->y·coord); + printf("\n%d rows in array\n",my_house_array->rows·in·array); + + +} + + +} \ No newline at end of file