diff options
| author | Bond_009 <bond.009@outlook.com> | 2022-12-01 22:30:22 +0100 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2022-12-01 22:30:22 +0100 |
| commit | baf4910870a6e8999802b9a4a22eabd4142a34e3 (patch) | |
| tree | 2d11443dc21e53bd0d99d015cf789937d6d95862 /2020/04 | |
| parent | 49d0c908f24b2c193c9deed1716fe36061ba26a1 (diff) | |
Move all Advent of Codes into one repo
Diffstat (limited to '2020/04')
| -rw-r--r-- | 2020/04/part1.c | 68 | ||||
| -rw-r--r-- | 2020/04/part2.c | 135 |
2 files changed, 203 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])); +} diff --git a/2020/04/part2.c b/2020/04/part2.c new file mode 100644 index 0000000..5aac219 --- /dev/null +++ b/2020/04/part2.c @@ -0,0 +1,135 @@ +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define KEYS_LEN 7 +const char* const keys[] = { "byr:", "iyr:", "eyr:", "hgt:", "hcl:", "ecl:", "pid:" }; + +bool is_valid_field(const char *key, const char *value) +{ + if (strncmp(key, "byr", 3) == 0) { + int v = atoi(value); + return v >= 1920 && v <= 2002; + } + + if (strncmp(key, "iyr", 3) == 0) { + int v = atoi(value); + return v >= 2010 && v <= 2020; + } + + if (strncmp(key, "eyr", 3) == 0) { + int v = atoi(value); + return v >= 2020 && v <= 2030; + } + + if (strncmp(key, "hgt", 3) == 0) { + int v = atoi(value); + if (strncmp(value + 3, "cm", 2) == 0) { + return v >= 150 && v <= 193; + } + + if (strncmp(value + 2, "in", 2) == 0) { + return v >= 59 && v <= 76; + } + + return false; + } + + if (strncmp(key, "hcl", 3) == 0) { + if (value[0] != '#') { + return false; + } + + for (int i = 1; i < 7; i++) { + if (!(value[i] >= 48 && value[i] <= 57) && !(value[i] >= 97 && value[i] <= 102)) { + return false; + } + } + + return true; + } + + if (strncmp(key, "ecl", 3) == 0) { + return strncmp(value, "amb", 3) == 0 + || strncmp(value, "blu", 3) == 0 + || strncmp(value, "brn", 3) == 0 + || strncmp(value, "gry", 3) == 0 + || strncmp(value, "grn", 3) == 0 + || strncmp(value, "hzl", 3) == 0 + || strncmp(value, "oth", 3) == 0; + } + + if (strncmp(key, "pid", 3) == 0) { + for (int i = 0; i < 9; i++) { + if (!(value[i] >= 48 && value[i] <= 57)) { + return false; + } + } + + return !(value[9] >= 48 && value[9] <= 57); + } + + return false; +} + +bool is_valid_passport(const char *pass) +{ + for (int i = 0; i < KEYS_LEN; i++) { + const char *p = strstr(pass, keys[i]); + if (!p) { + return false; + } + + if (!is_valid_field(p, p + 4)) { + return false; + } + } + + return true; +} + +int count_valid_passports(const char *filename) +{ + FILE *file = fopen(filename, "r"); + + // Include space for newline and string terminator + char buffer[128] = { 0 }; + + uint8_t values = 0; + int correct = 0; + while (fgets(buffer, 128, file)) { + if (buffer[0] == '\n') { + if (values == 0x7f) { + correct++; + } + + values = 0; + } + + for (int i = 0; i < KEYS_LEN; i++) { + const char *p = strstr(buffer, keys[i]); + if (!p) { + continue; + } + + if (is_valid_field(p, p + 4)) { + values = values | (0x1 << i); + } + } + } + + if (values == 0x7f) { + correct++; + } + + fclose(file); + + return correct; +} + +int main(int argc, char *argv[]) +{ + printf("%i", count_valid_passports(argv[argc - 1])); +} |
