diff options
| author | Bond_009 <bond.009@outlook.com> | 2020-12-02 19:39:20 +0100 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2020-12-02 19:39:20 +0100 |
| commit | 928092a7b82bc6dd16926a378b91e24510b9ec0f (patch) | |
| tree | 5d4e7e3562abe75ba672866befbc8880ce5867c8 /2/part1.c | |
| parent | 805e29bd5ddafaa92ec5dd889f9989b307e6849c (diff) | |
Add day 2
Diffstat (limited to '2/part1.c')
| -rw-r--r-- | 2/part1.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/2/part1.c b/2/part1.c new file mode 100644 index 0000000..ddac78e --- /dev/null +++ b/2/part1.c @@ -0,0 +1,35 @@ +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +bool is_valid_password(char * pass, char * policy) +{ + int min = atoi(policy); + char * minP = strchr(policy, '-'); + int max = atoi(++minP); + char c = *(strchr(minP, ' ') + 1); + char * p = pass; + int occ = 0; + do { + if (*p == c) { + occ++; + } + } while (*++p); + + return occ >= min && occ <= max; +} + +int main() +{ + FILE * file = fopen("input", "r"); + char buffer[128] = { 0 }; + int valid = 0; + while (fgets(buffer, 128, file)) { + if (is_valid_password(strchr(buffer, ':') + 2, buffer)) { + valid++; + } + } + + printf("%i", valid); +} |
