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/part2.c | |
| parent | 805e29bd5ddafaa92ec5dd889f9989b307e6849c (diff) | |
Add day 2
Diffstat (limited to '2/part2.c')
| -rw-r--r-- | 2/part2.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/2/part2.c b/2/part2.c new file mode 100644 index 0000000..91165dc --- /dev/null +++ b/2/part2.c @@ -0,0 +1,28 @@ +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +bool is_valid_password(char * pass, char * policy) +{ + int pos1 = atoi(policy) - 1; + char * minP = strchr(policy, '-'); + int pos2 = atoi(++minP) - 1; + char c = *(strchr(minP, ' ') + 1); + + return (pass[pos1] == c) != (pass[pos2] == c); +} + +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); +} |
