167 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			167 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| #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_origin;
 | |
|   int y_coord_origin;
 | |
|   int seq·no;
 | |
| } Housearray;
 | |
| 
 | |
| //initial array allocation
 | |
| Housearray *inithousearray(void){
 | |
|   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->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;
 | |
|   }
 | |
|   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, char direction){
 | |
|   int old_rows = f->rows·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
 | |
|   f->y·coord--;
 | |
|   //check if expansion needed
 | |
|   if (f->y·coord < 0){
 | |
|     //Expand north
 | |
|     int new_rows = old_rows +1;
 | |
|     int new_cols = old_cols;
 | |
|     //creates am 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+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 = f->x·coord - f->x_coord_origin;
 | |
|   int grid_y = f->y·coord - f->y_coord_origin;
 | |
|   f->houses[grid_y][grid_x]++;
 | |
| }
 | |
| 
 | |
| void movesouth(Housearray* f, char direction){
 | |
|   int old_rows = f->rows·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
 | |
|   f->y·coord++;
 | |
|   //check if expansion needed
 | |
|   if (f->y·coord > 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[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 = 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();
 | |
|   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;
 | |
|   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, ch);
 | |
|       }else if (ch == down){
 | |
|         printf("\nDOWN");
 | |
|         movesouth(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);
 | |
| 
 | |
| 
 | |
| }
 | |
| 
 | |
| 
 | |
| } |