main.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define IMP
  5. #include "../file.h"
  6. struct report {
  7. size_t cap;
  8. size_t size;
  9. long *data;
  10. };
  11. size_t cstr_char_count(const char *cstr, char c);
  12. int cmp_sort(const void *this, const void *other);
  13. int
  14. main(int argc, char *argv[])
  15. {
  16. enum file_err ferr;
  17. size_t line_num = 0;
  18. size_t i = 0;
  19. long res = 0;
  20. struct report *reports = NULL;
  21. size_t reports_size = 0;
  22. char *raw_input = NULL;
  23. size_t raw_input_size = 0;
  24. raw_input = (char *) file_read_all("./input.txt",
  25. &raw_input_size,
  26. &ferr);
  27. if ( ferr != FILE_ERR_OK ) {
  28. fprintf(stderr, "Failed to open file: %s\n",
  29. file_err_to_cstr(ferr));
  30. exit(EXIT_FAILURE);
  31. }
  32. line_num = cstr_char_count(raw_input, '\n');
  33. reports = calloc(line_num+1, sizeof(*reports));
  34. {
  35. long ret = -1;
  36. char *str = raw_input;
  37. char *nstr = NULL;
  38. struct report *this = &reports[reports_size++];
  39. if ( this->data == NULL ) {
  40. this->cap = 10;
  41. this->size = 0;
  42. this->data = calloc(this->cap, sizeof(*this->data));
  43. }
  44. while ( true ) {
  45. ret = strtol(str, &nstr, 10);
  46. if ( ret == 0 ) {
  47. break;
  48. }
  49. str = nstr;
  50. if ( this->size+1 >= this->cap ) {
  51. this->cap <<= 1;
  52. this->data = realloc(this->data, this->cap * sizeof(*this->data));
  53. }
  54. this->data[this->size++] = ret;
  55. if ( *str == '\n' ) {
  56. this = &reports[reports_size++];
  57. if ( this->data == NULL ) {
  58. this->cap = 10;
  59. this->size = 0;
  60. this->data = calloc(this->cap, sizeof(*this->data));
  61. }
  62. }
  63. }
  64. }
  65. if ( argc == 1 ) {
  66. for ( i = 0; i < reports_size; ++i ) {
  67. size_t j = 1;
  68. struct report *this = &reports[i];
  69. long dir = this->data[0] - this->data[1];
  70. if ( dir == 0 ) {
  71. /* if dir is == 0 it is already unsafe */
  72. continue;
  73. }
  74. ++res;
  75. for ( j = 1; j < this->size; ++j ) {
  76. long diff = this->data[j-1] - this->data[j];
  77. long diff_abs = labs(diff);
  78. if ( diff_abs < 1 || diff_abs > 3 ) {
  79. --res;
  80. break;
  81. }
  82. if ( ( diff > 0 && dir < 0 ) || ( diff < 0 && dir > 0 ) ) {
  83. --res;
  84. break;
  85. }
  86. }
  87. }
  88. } else {
  89. }
  90. printf("Result: %ld\n", res);
  91. for ( i = 0; i < reports_size; ++i ) {
  92. free(reports[i].data);
  93. }
  94. free(reports);
  95. free(raw_input);
  96. (void) argc; (void) argv;
  97. return 0;
  98. }
  99. size_t
  100. cstr_char_count(const char *cstr, char c)
  101. {
  102. size_t ret = 0;
  103. while ( *cstr != '\0' ) {
  104. ret += ( *cstr == c );
  105. cstr++;
  106. }
  107. return ret;
  108. }
  109. int
  110. cmp_sort(const void *this, const void *other)
  111. {
  112. long this_ = *((long*) this);
  113. long other_ = *((long*) other);
  114. return (int) (this_ - other_);
  115. }