#include #include #include #include int32_t compint(const void *a, const void *b) { uint16_t tmp_a = *((uint16_t *)a); uint16_t tmp_b = *((uint16_t *)b); if (tmp_a == tmp_b) return 0; else if (tmp_a > tmp_b) return 1; else return -1; } int main() { FILE *file = fopen("input", "r"); if (file == NULL) { perror("main: Error opening file."); return 1; } uint16_t l, w, h, sa_tmp; uint32_t sa_cum = 0; int8_t buffer[256]; while (fgets(buffer, sizeof(buffer), file) != NULL) { if (sscanf(buffer, "%hux%hux%hu", &l, &w, &h) != 3) { perror("main: Error parsing string."); return 1; } else { //calculate surface area sa_tmp = 2*l*w + 2*w*h + 2*l*h; //calculate smallest side uint16_t array[3] = {l, w, h}; uint16_t n = sizeof(array)/sizeof(array[0]); qsort(array, n, sizeof(array[0]), compint); //calculate cumulative surface area sa_cum = sa_cum + sa_tmp + (array[0]*array[1]); } } if (ferror(file)) { perror("main: Error reading from file."); return 1; } fclose(file); printf("cumulative surface area: %u\n", sa_cum); return 0; }