diff --git a/2020/2/parse b/2020/2/parse index beaf00f..0aa5ddc 100755 Binary files a/2020/2/parse and b/2020/2/parse differ diff --git a/2020/2/parse.c b/2020/2/parse.c index cf4534d..99f05b0 100644 --- a/2020/2/parse.c +++ b/2020/2/parse.c @@ -4,25 +4,27 @@ int main(void) { FILE *fp; - char* line = NULL; - size_t len = 0; + char line[255]; int valid = 0; fp = fopen("input", "r"); - while(getline(&line, &len, fp) != -1) { + if(fp == NULL) { + perror("Error opening file."); + return(-1); + } + + while(fgets(line, sizeof(line), fp) != NULL) { int start, end; char subject; - char* password; + char password[64]; sscanf(line, "%d-%d %c: %s\n", &start, &end, &subject, password); - int occ = 0; - for(int i=0; password[i] != 0; i++) { + for(int i=0; password[i]; i++) if(password[i] == subject) occ++; - } - + if(occ <= end && occ >= start) valid++; } diff --git a/2020/2/parse_idk_two b/2020/2/parse_idk_two index 7510ae3..d605bdb 100755 Binary files a/2020/2/parse_idk_two and b/2020/2/parse_idk_two differ diff --git a/2020/2/parse_idk_two.c b/2020/2/parse_idk_two.c index a1f8f95..3d8ccc9 100644 --- a/2020/2/parse_idk_two.c +++ b/2020/2/parse_idk_two.c @@ -3,16 +3,20 @@ int main(void) { FILE *fp; - char* line = NULL; - size_t len = 0; + char line[255]; int valid = 0; fp = fopen("input", "r"); - - while(getline(&line, &len, fp) != -1) { + + if(fp == NULL) { + perror("Error opening file."); + return(-1); + } + + while(fgets(line, sizeof(line), fp) != NULL) { int start, end; char subject; - char* password; + char password[64]; sscanf(line, "%d-%d %c: %s\n", &start, &end, &subject, password);