adventofcode/2020/2/parse.c
2022-12-01 21:01:59 +01:00

33 lines
546 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *fp;
char* line = NULL;
size_t len = 0;
int valid = 0;
fp = fopen("input", "r");
while(getline(&line, &len, fp) != -1) {
int start, end;
char subject;
char* password;
sscanf(line, "%d-%d %c: %s\n", &start, &end, &subject, password);
int occ = 0;
for(int i=0; password[i] != 0; i++) {
if(password[i] == subject) occ++;
}
if(occ <= end && occ >= start) valid++;
}
fclose(fp);
printf("There are %d valid passwords\n", valid);
}