diff --git a/2015_day_3/day_3 b/2015_day_3/day_3 new file mode 100755 index 0000000..dc1402e Binary files /dev/null and b/2015_day_3/day_3 differ diff --git a/2015_day_3/day_3.c b/2015_day_3/day_3.c index 033f88b..e1b4bcb 100644 --- a/2015_day_3/day_3.c +++ b/2015_day_3/day_3.c @@ -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); }