main.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define IMP
  5. #include "../../file.h"
  6. #include "../../str.h"
  7. #include "../../branchless.h"
  8. struct rect {
  9. long l;
  10. long w;
  11. long h;
  12. };
  13. int
  14. main(int argc, char *argv[])
  15. {
  16. enum file_err ferr;
  17. long res = 0;
  18. size_t i = 0;
  19. struct rect r = {0};
  20. struct str str_input = {0};
  21. size_t num_lines = 0;
  22. struct rect *rects = NULL;
  23. size_t rects_size = 0;
  24. char *raw_input = NULL;
  25. size_t raw_input_size = 0;
  26. raw_input = (char *) file_read_all("./input.txt",
  27. &raw_input_size,
  28. &ferr);
  29. if ( ferr != FILE_ERR_OK ) {
  30. fprintf(stderr, "Failed to open file: %s\n",
  31. file_err_to_cstr(ferr));
  32. exit(EXIT_FAILURE);
  33. }
  34. str_input = str_from_cstr(raw_input, raw_input_size);
  35. num_lines = str_count_char(str_input, '\n');
  36. rects = calloc(num_lines, sizeof(*rects));
  37. {
  38. struct str_tokenizer st = str_tokenize(str_input, '\n');
  39. struct str str = str_tokenizer_next(&st);
  40. while ( str.size != (size_t) -1 ) {
  41. char *nstr = (char *) str.data;
  42. r.l = strtol(nstr, &nstr, 10);
  43. r.w = strtol(++nstr, &nstr, 10);
  44. r.h = strtol(++nstr, NULL, 10);
  45. rects[rects_size++] = r;
  46. str = str_tokenizer_next(&st);
  47. }
  48. }
  49. if ( argc == 1 ) {
  50. long s1 = 0;
  51. long s2 = 0;
  52. long s3 = 0;
  53. long m1 = 0;
  54. for ( i = 0; i < rects_size; ++i ) {
  55. r = rects[i];
  56. s1 = r.l * r.w;
  57. s2 = r.w * r.h;
  58. s3 = r.h * r.l;
  59. m1 = BL_MIN(s2, s3);
  60. res += (s1 * 2) + (s2 * 2) + (s3 * 2) + BL_MIN(s1, m1);
  61. }
  62. } else {
  63. long m1 = 0;
  64. long m2 = 0;
  65. long ma1 = 0;
  66. for ( i = 0; i < rects_size; ++i ) {
  67. r = rects[i];
  68. m1 = BL_MIN(r.l, r.w);
  69. ma1 = BL_MAX(r.l, r.w);
  70. m2 = BL_MIN(r.h, ma1);
  71. res += ((m1 + m1 + m2 + m2) + (r.l * r.w * r.h));
  72. }
  73. }
  74. printf("Result: %ld\n", res);
  75. free(rects);
  76. free(raw_input);
  77. (void) argc; (void) argv;
  78. return 0;
  79. }