358 lines
10 KiB
C
358 lines
10 KiB
C
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
#include<string.h>
|
|
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·bot;
|
|
int y·coord·bot;
|
|
int x_coord_origin;
|
|
int y_coord_origin;
|
|
int seq·no;
|
|
} Housearray;
|
|
|
|
//initial array allocation
|
|
Housearray *inithousearray(int bot){
|
|
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->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
|
|
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;
|
|
}
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
|
|
//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, 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
|
|
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 (north_y - f->y_coord_origin < 0){
|
|
//Expand north
|
|
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*));
|
|
if (new_grid == NULL){
|
|
return;
|
|
}
|
|
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 = 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, 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
|
|
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 (south_y - 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 = 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, 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++;
|
|
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 (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*));
|
|
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[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);
|
|
f->houses = new_grid;
|
|
f->rows·in·array = new_rows;
|
|
f->columns·in·array = new_cols;
|
|
|
|
}
|
|
//updating the visit count
|
|
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, int user){
|
|
int old_rows = f->rows·in·array;
|
|
int old_cols = f-> columns·in·array;
|
|
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 (west_x- f->x_coord_origin < 0){
|
|
//Expand south
|
|
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], 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];
|
|
}
|
|
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 = west_x - f->x_coord_origin;
|
|
int grid_y = west_y- f->y_coord_origin;
|
|
f->houses[grid_y][grid_x]++;
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
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;
|
|
}
|
|
// char c;
|
|
char up = '^';
|
|
char down = 'v';
|
|
char left = '<';
|
|
char right = '>';
|
|
|
|
char ch, zh;
|
|
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, 0);
|
|
}else if (ch == down){
|
|
printf("\nDOWN");
|
|
movesouth(my_house_array,0);
|
|
}else if (ch == left){
|
|
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);
|
|
} |