adventofcode/2020/2/parse.c

35 lines
594 B
C
Raw Normal View History

2022-12-01 21:01:59 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *fp;
2022-12-01 21:01:59 +01:00
char line[255];
2022-12-01 21:01:59 +01:00
int valid = 0;
fp = fopen("input", "r");
2022-12-01 21:01:59 +01:00
if(fp == NULL) {
perror("Error opening file.");
return(-1);
}
while(fgets(line, sizeof(line), fp) != NULL) {
2022-12-01 21:01:59 +01:00
int start, end;
char subject;
2022-12-01 21:01:59 +01:00
char password[64];
2022-12-01 21:01:59 +01:00
sscanf(line, "%d-%d %c: %s\n", &start, &end, &subject, password);
int occ = 0;
2022-12-01 21:01:59 +01:00
for(int i=0; password[i]; i++)
2022-12-01 21:01:59 +01:00
if(password[i] == subject) occ++;
2022-12-01 21:01:59 +01:00
2022-12-01 21:01:59 +01:00
if(occ <= end && occ >= start) valid++;
}
fclose(fp);
printf("There are %d valid passwords\n", valid);
}