94 lines
3.8 KiB
C
94 lines
3.8 KiB
C
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
#include<string.h>
|
|
FILE *fptr;
|
|
|
|
struct dim_struct{
|
|
int dimensions[3];
|
|
int surfaces[3];
|
|
int smallest_side;
|
|
int smallest_side_id;
|
|
int smallest_side_perimeter;
|
|
int box_volume;
|
|
int total_ribbon_for_item;
|
|
int total_paper_for_item;
|
|
};// Fun fact: C expects a semicolon after the struct definition
|
|
|
|
int main(){
|
|
struct dim_struct myArray[1005];
|
|
fptr = fopen("2015_day_2_input.txt", "r");
|
|
if(fptr == NULL) {
|
|
printf("Not able to open the file.");
|
|
return 1;
|
|
}
|
|
printf("hello\n");
|
|
long int line_count = 0;
|
|
int dim = 0;
|
|
int matrix[3][2] = { {0,1}, {1,2}, {2,0} };
|
|
int i;
|
|
int dim1, dim2;
|
|
char buffer[266];
|
|
while (fgets(buffer, sizeof(buffer), fptr)) {
|
|
printf("%s\n",buffer);
|
|
char delimiter = 'x';
|
|
char *token, *saveptr;
|
|
line_count++;
|
|
for (token = strtok_r(buffer, &delimiter, &saveptr); token != NULL; token = strtok_r(NULL, &delimiter, &saveptr)) {
|
|
printf("%s\n", token); // or process token
|
|
myArray[line_count].dimensions[dim] = atoi(token);
|
|
printf("\nwow\n%d\nwow\n",myArray[line_count].dimensions[dim]);
|
|
dim++;
|
|
}
|
|
memset(buffer, 0, sizeof(buffer)); // clear the buffer to zero
|
|
dim = 0;
|
|
}
|
|
|
|
printf("\n length of myArray = %lu\n", sizeof(myArray));
|
|
for (i=0; i<line_count+1; i++){
|
|
myArray[i].surfaces[0] = myArray[i].dimensions[0] * myArray[i].dimensions[1];
|
|
myArray[i].surfaces[1] = myArray[i].dimensions[1] * myArray[i].dimensions[2];
|
|
myArray[i].surfaces[2] = myArray[i].dimensions[2] * myArray[i].dimensions[0];
|
|
if ((myArray[i].surfaces[0] <= myArray[i].surfaces[1]) && (myArray[i].surfaces[0] <= myArray[i].surfaces[2])){
|
|
myArray[i].smallest_side = myArray[i].surfaces[0];
|
|
myArray[i].smallest_side_id = 0;
|
|
}
|
|
else if ((myArray[i].surfaces[1] <= myArray[i].surfaces[0]) && (myArray[i].surfaces[1] <= myArray[i].surfaces[2])){
|
|
myArray[i].smallest_side = myArray[i].surfaces[1];
|
|
myArray[i].smallest_side_id = 1;
|
|
}
|
|
else if ((myArray[i].surfaces[2] <= myArray[i].surfaces[0]) && (myArray[i].surfaces[2] <= myArray[i].surfaces[1])){
|
|
myArray[i].smallest_side = myArray[i].surfaces[2];
|
|
myArray[i].smallest_side_id = 2;
|
|
}
|
|
else {
|
|
printf("ERROR!");
|
|
exit(1);
|
|
}
|
|
|
|
myArray[i].total_paper_for_item = ((2 * (myArray[i].surfaces[0] + myArray[i].surfaces[1] + myArray[i].surfaces[2])) + myArray[i].smallest_side);
|
|
printf("\ntotal paper for item = %d\n", myArray[i].total_paper_for_item);
|
|
printf("surface 0 is %d, %d times %d\n", myArray[i].surfaces[0], myArray[i].dimensions[0] , myArray[i].dimensions[1]);
|
|
printf("surface 1 is %d, %d times %d\n", myArray[i].surfaces[1], myArray[i].dimensions[1] , myArray[i].dimensions[2]);
|
|
printf("surface 2 is %d, %d times %d\n", myArray[i].surfaces[2], myArray[i].dimensions[2] , myArray[i].dimensions[0]);
|
|
printf("The smallest side of item %d is %d\n",i ,myArray[i].smallest_side);
|
|
|
|
|
|
myArray[i].smallest_side_perimeter = (2 * myArray[i].dimensions[matrix[myArray[i].smallest_side_id][0]]) + (2 * myArray[i].dimensions[matrix[myArray[i].smallest_side_id][1]]); //don't love this, but it seems to be a fairly simple way to do it
|
|
printf("\n perimeter of smallest side = %d\n", myArray[i].smallest_side_perimeter);
|
|
myArray[i].box_volume = myArray[i].dimensions[0] * (myArray[i].dimensions[1] * myArray[i].dimensions[2]);
|
|
myArray[i].total_ribbon_for_item = myArray[i].smallest_side_perimeter + myArray[i].box_volume;
|
|
|
|
}
|
|
for (i=0; i<line_count+1; i++){
|
|
dim1 = dim1 + myArray[i].total_paper_for_item;
|
|
dim2 = dim2 + myArray[i].total_ribbon_for_item;
|
|
|
|
}
|
|
|
|
fclose(fptr);
|
|
printf("\n\n Total paper needed: %d \n\n", dim1);
|
|
printf("\n\n Total ribbon needed: %d \n\n", dim2);
|
|
|
|
return 0;
|
|
}
|
|
//https://www.geeksforgeeks.org/how-to-create-an-array-of-structs-in-c/
|