| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define IMP
- #include "../file.h"
- size_t cstr_char_count(const char *cstr, char c);
- int cmp_sort(const void *this, const void *other);
- int
- main(int argc, char *argv[])
- {
- enum file_err ferr;
- long res = 0;
- char *raw_input = NULL;
- size_t raw_input_size = 0;
- raw_input = (char *) file_read_all("./input.txt",
- &raw_input_size,
- &ferr);
- if ( ferr != FILE_ERR_OK ) {
- fprintf(stderr, "Failed to open file: %s\n",
- file_err_to_cstr(ferr));
- exit(EXIT_FAILURE);
- }
- if ( argc == 1 ) {
- char *cstr = raw_input;
- loop_p1:
- switch ( *(cstr++) ) {
- case '\0':
- goto exit_loop;
- case '(':
- ++res;
- break;
- case ')':
- --res;
- break;
- }
- goto loop_p1;
- } else {
- size_t i = 0;
- char *cstr = raw_input;
- loop_p2:
- switch ( *(cstr + i) ) {
- case '\0':
- goto exit_loop;
- case '(':
- ++res;
- break;
- case ')':
- --res;
- if ( res < 0 ) {
- res = ((long) i) + 1;
- goto exit_loop;
- }
- break;
- }
- ++i;
- goto loop_p2;
- }
- exit_loop:
- printf("Result: %ld\n", res);
- free(raw_input);
- (void) argc; (void) argv;
- return 0;
- }
- size_t
- cstr_char_count(const char *cstr, char c)
- {
- size_t ret = 0;
- while ( *cstr != '\0' ) {
- ret += ( *cstr == c );
- cstr++;
- }
- return ret;
- }
- int
- cmp_sort(const void *this, const void *other)
- {
- long this_ = *((long*) this);
- long other_ = *((long*) other);
- return (int) (this_ - other_);
- }
|