This commit is contained in:
ThomasBallantine 2025-08-19 22:04:38 +01:00
parent 5bac6275ef
commit 4165228828
2 changed files with 66 additions and 0 deletions

BIN
2015_day_3/day_3 Executable file

Binary file not shown.

View File

@ -128,6 +128,67 @@ void movesouth(Housearray* f, char direction){
f->houses[grid_y][grid_x]++;
}
void moveeast(Housearray* f, char direction){
int old_rows = f->rows·in·array;
int old_cols = f-> columns·in·array;
//update x coordinate
f->x·coord++;
//check if expansion needed
if (f->x·coord < 0){
//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*));
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_grid[i] = new_row;
//new_grid[i] = f->houses[i];
}
free(f->houses);
f->houses = new_grid;
f->rows·in·array = new_rows;
f->columns·in·array = new_cols;
}
//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 movewest(Housearray* f, char direction){
int old_rows = f->rows·in·array;
int old_cols = f-> columns·in·array;
//update y coordinate
f->y·coord++;
//check if expansion needed
if (f->y·coord - f->y_coord_origin >= 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[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;
}
//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();
@ -156,9 +217,14 @@ int main(){
}else if (ch == down){
printf("\nDOWN");
movesouth(my_house_array, ch);
}else if (ch == left){
printf("\nEAST");
moveeast(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);
printf("\n%d cols in array\n",my_house_array->columns·in·array);
}