summaryrefslogtreecommitdiff
path: root/2020/04/part1.c
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2022-12-01 22:30:22 +0100
committerBond_009 <bond.009@outlook.com>2022-12-01 22:30:22 +0100
commitbaf4910870a6e8999802b9a4a22eabd4142a34e3 (patch)
tree2d11443dc21e53bd0d99d015cf789937d6d95862 /2020/04/part1.c
parent49d0c908f24b2c193c9deed1716fe36061ba26a1 (diff)
Move all Advent of Codes into one repo
Diffstat (limited to '2020/04/part1.c')
-rw-r--r--2020/04/part1.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/2020/04/part1.c b/2020/04/part1.c
new file mode 100644
index 0000000..b4a3b8e
--- /dev/null
+++ b/2020/04/part1.c
@@ -0,0 +1,68 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+bool is_valid_passport(const char *pass)
+{
+ return strstr(pass, "byr:")
+ && strstr(pass, "iyr:")
+ && strstr(pass, "eyr:")
+ && strstr(pass, "hgt:")
+ && strstr(pass, "hcl:")
+ && strstr(pass, "ecl:")
+ && strstr(pass, "pid:");
+}
+
+int count_valid_passports(const char *filename)
+{
+ FILE *file = fopen(filename, "r");
+
+ // Include space for newline and string terminator
+ char buffer[128] = { 0 };
+
+ bool has_byr = false;
+ bool has_iyr = false;
+ bool has_eyr = false;
+ bool has_hgt = false;
+ bool has_hcl = false;
+ bool has_ecl = false;
+ bool has_pid = false;
+ int correct = 0;
+ while (fgets(buffer, 128, file)) {
+ if (buffer[0] == '\n') {
+ if (has_byr && has_iyr && has_eyr && has_hgt && has_hcl && has_ecl && has_pid) {
+ correct++;
+ }
+
+ has_byr = false;
+ has_iyr = false;
+ has_eyr = false;
+ has_hgt = false;
+ has_hcl = false;
+ has_ecl = false;
+ has_pid = false;
+ }
+
+ has_byr = has_byr || strstr(buffer, "byr:");
+ has_iyr = has_iyr || strstr(buffer, "iyr:");
+ has_eyr = has_eyr || strstr(buffer, "eyr:");
+ has_hgt = has_hgt || strstr(buffer, "hgt:");
+ has_hcl = has_hcl || strstr(buffer, "hcl:");
+ has_ecl = has_ecl || strstr(buffer, "ecl:");
+ has_pid = has_pid || strstr(buffer, "pid:");
+ }
+
+ if (has_byr && has_iyr && has_eyr && has_hgt && has_hcl && has_ecl && has_pid) {
+ correct++;
+ }
+
+ fclose(file);
+
+ return correct;
+}
+
+int main(int argc, char *argv[])
+{
+ printf("%i", count_valid_passports(argv[argc - 1]));
+}