main.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #define IMP
  5. #include "../file.h"
  6. size_t cstr_char_count(const char *cstr, char c);
  7. int cmp_sort(const void *this, const void *other);
  8. int
  9. main(int argc, char *argv[])
  10. {
  11. enum file_err ferr;
  12. long res = 0;
  13. char *raw_input = NULL;
  14. size_t raw_input_size = 0;
  15. raw_input = (char *) file_read_all("./input.txt",
  16. &raw_input_size,
  17. &ferr);
  18. if ( ferr != FILE_ERR_OK ) {
  19. fprintf(stderr, "Failed to open file: %s\n",
  20. file_err_to_cstr(ferr));
  21. exit(EXIT_FAILURE);
  22. }
  23. if ( argc == 1 ) {
  24. char *cstr = raw_input;
  25. loop_p1:
  26. switch ( *(cstr++) ) {
  27. case '\0':
  28. goto exit_loop;
  29. case '(':
  30. ++res;
  31. break;
  32. case ')':
  33. --res;
  34. break;
  35. }
  36. goto loop_p1;
  37. } else {
  38. size_t i = 0;
  39. char *cstr = raw_input;
  40. loop_p2:
  41. switch ( *(cstr + i) ) {
  42. case '\0':
  43. goto exit_loop;
  44. case '(':
  45. ++res;
  46. break;
  47. case ')':
  48. --res;
  49. if ( res < 0 ) {
  50. res = ((long) i) + 1;
  51. goto exit_loop;
  52. }
  53. break;
  54. }
  55. ++i;
  56. goto loop_p2;
  57. }
  58. exit_loop:
  59. printf("Result: %ld\n", res);
  60. free(raw_input);
  61. (void) argc; (void) argv;
  62. return 0;
  63. }
  64. size_t
  65. cstr_char_count(const char *cstr, char c)
  66. {
  67. size_t ret = 0;
  68. while ( *cstr != '\0' ) {
  69. ret += ( *cstr == c );
  70. cstr++;
  71. }
  72. return ret;
  73. }
  74. int
  75. cmp_sort(const void *this, const void *other)
  76. {
  77. long this_ = *((long*) this);
  78. long other_ = *((long*) other);
  79. return (int) (this_ - other_);
  80. }