updates to elimate unused variables and enhance readme
This commit is contained in:
parent
a81ed99e82
commit
5bac6275ef
Binary file not shown.
@ -21,7 +21,9 @@ Adding South rows ought to be simple, as that's just pushing all the values furt
|
|||||||
https://newton.ex.ac.uk/teaching/resources/jmr/appendix-growable.html
|
https://newton.ex.ac.uk/teaching/resources/jmr/appendix-growable.html
|
||||||
Upon adding this, I figured that it would be best to adopt a struct to copy the array between instances of it
|
Upon adding this, I figured that it would be best to adopt a struct to copy the array between instances of it
|
||||||
|
|
||||||
Had a chat with am AI, it reckoned that the thing to do was to expand the array by a row or column when Santa was detected as being out of bounds
|
So, it seems that really what I'll be doing is managing arrays of pointers for rows, which is good, as it means that I can let the memory management do its thing without me having to track whether or not I've got a continuous region of memory. I'm also having Santa start out at a 1x1 grid. So I'm adding rows when he's detected out of bounds, by tracking the grid size, his location on the grid, his location relative to the origin and having it add rows when he's above or below the current grid (started off in the vertical axis as it seemed easiest). Had some difficulties with the logic of tracking where Santa is on the grid, took a little while to come to the correct calculations there.
|
||||||
|
|
||||||
|
I really like passing the pointer to the struct to the function in C, it makes things quite elegant (and easier for memory management!) as you're updating fields in a struct rather than creating a new copy each time. There's probably performance benefits, but this is so tiny that I'm unlikely to have to think about that. The new_grid pointers being inserted into the ->houses slot is handy. Did I say I really like only having one struct in play?
|
||||||
|
|
||||||
https://adventofcode.com/2015/day/3
|
https://adventofcode.com/2015/day/3
|
||||||
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
|
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
|
||||||
|
@ -71,8 +71,6 @@ Housearray *inithousearray(void){
|
|||||||
void movenorth(Housearray* f, char direction){
|
void movenorth(Housearray* f, char direction){
|
||||||
int old_rows = f->rows·in·array;
|
int old_rows = f->rows·in·array;
|
||||||
int old_cols = f-> columns·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
|
//update y coordinate
|
||||||
f->y·coord--;
|
f->y·coord--;
|
||||||
//check if expansion needed
|
//check if expansion needed
|
||||||
@ -80,7 +78,7 @@ void movenorth(Housearray* f, char direction){
|
|||||||
//Expand north
|
//Expand north
|
||||||
int new_rows = old_rows +1;
|
int new_rows = old_rows +1;
|
||||||
int new_cols = old_cols;
|
int new_cols = old_cols;
|
||||||
//creates am array of pointers to point to my rows of integers
|
//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*));
|
||||||
if (new_grid == NULL){
|
if (new_grid == NULL){
|
||||||
return;
|
return;
|
||||||
@ -105,8 +103,6 @@ void movenorth(Housearray* f, char direction){
|
|||||||
void movesouth(Housearray* f, char direction){
|
void movesouth(Housearray* f, char direction){
|
||||||
int old_rows = f->rows·in·array;
|
int old_rows = f->rows·in·array;
|
||||||
int old_cols = f-> columns·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
|
//update y coordinate
|
||||||
f->y·coord++;
|
f->y·coord++;
|
||||||
//check if expansion needed
|
//check if expansion needed
|
||||||
@ -139,7 +135,7 @@ int main(){
|
|||||||
fprintf(stderr, "ERROR:initialisation of struct failed\n");
|
fprintf(stderr, "ERROR:initialisation of struct failed\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
char c;
|
// char c;
|
||||||
char up = '^';
|
char up = '^';
|
||||||
char down = 'v';
|
char down = 'v';
|
||||||
char left = '<';
|
char left = '<';
|
||||||
|
@ -115,7 +115,7 @@ void movesouth(Housearray* f, char direction){
|
|||||||
for (int i = 0; i< old_rows; ++i){
|
for (int i = 0; i< old_rows; ++i){
|
||||||
new_grid[i] = f->houses[i];
|
new_grid[i] = f->houses[i];
|
||||||
}
|
}
|
||||||
new_grid[0] = (int*)calloc(new_cols, sizeof(int));//Initialising the new row to empty
|
new_grid[new_rows - 1] = (int*)calloc(new_cols, sizeof(int));//Initialising the new row to empty
|
||||||
free(f->houses);
|
free(f->houses);
|
||||||
f->houses = new_grid;
|
f->houses = new_grid;
|
||||||
f->rows·in·array = new_rows;
|
f->rows·in·array = new_rows;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user