#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, sp_tmp; uint32_t ribbon = 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 shortest perimeter uint16_t array[3] = {l, w, h}; uint16_t n = sizeof(array)/sizeof(array[0]); qsort(array, n, sizeof(array[0]), compint); sp_tmp = 2*array[0] + 2*array[1]; //calculate cubic volume uint16_t cv = l*w*h; //calculate cumulative ribbon length ribbon = ribbon + sp_tmp + cv; } } if (ferror(file)) { perror("main: Error reading from file."); return 1; } fclose(file); printf("total ribbon length: %u\n", ribbon); return 0; }