From baf4910870a6e8999802b9a4a22eabd4142a34e3 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Thu, 1 Dec 2022 22:30:22 +0100 Subject: Move all Advent of Codes into one repo --- 2020/02/part2.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2020/02/part2.c (limited to '2020/02/part2.c') diff --git a/2020/02/part2.c b/2020/02/part2.c new file mode 100644 index 0000000..7a2e16e --- /dev/null +++ b/2020/02/part2.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +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(int argc, char *argv[]) +{ + FILE *file = fopen(argv[argc - 1], "r"); + if (!file) { + return 1; + } + + char buffer[128] = { 0 }; + int valid = 0; + while (fgets(buffer, 128, file)) { + if (is_valid_password(strchr(buffer, ':') + 2, buffer)) { + valid++; + } + } + + fclose(file); + + printf("%i", valid); +} -- cgit v1.2.3