Adding the completion of day 3
This commit is contained in:
@@ -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);
|
||||
}
|
Reference in New Issue
Block a user