From 479a2932e9e8a6967fce0bfd70ea82d26530dbb8 Mon Sep 17 00:00:00 2001 From: Shanti Chellaram Date: Sat, 23 Oct 2021 12:51:41 -0400 Subject: [PATCH] Add problem 4 --- .gitignore | 1 + c++/main.cpp | 2 +- c++/problem4.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++ c++/solutions.hpp | 1 + 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 c++/problem4.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03319c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/c++/aoc-cpp diff --git a/c++/main.cpp b/c++/main.cpp index a85906e..eee56c7 100644 --- a/c++/main.cpp +++ b/c++/main.cpp @@ -11,7 +11,7 @@ int main(int argc, char *argv[]) { return 0; } int problem_number = stoi(string(argv[1])); - solution_fn solutions[] = {solve1, solve2, solve3}; + solution_fn solutions[] = {solve1, solve2, solve3, solve4}; auto x = solutions[problem_number-1]; auto answer = x(cin); cout << "Solution 1: " << answer.first << endl; diff --git a/c++/problem4.cpp b/c++/problem4.cpp new file mode 100644 index 0000000..0b58897 --- /dev/null +++ b/c++/problem4.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "solutions.hpp" + +using namespace std; + +bool is_digit(char c) { + return isdigit((int) c); +} + +bool is_hex_digit(char c) { + return isdigit((int) c) || (c >= 'a' && c <= 'f'); +} + +pair solve4(istream& input) { + cerr << boolalpha; + stringstream current_passport{string()}; + uint64_t part1 = 0, part2 = 0; + unordered_map> valid_funcs; + valid_funcs["byr"] = [](string &value) { + int int_value = stoi(value); + return value.length() == 4 && all_of(value.begin(), value.end(), is_digit) && int_value >= 1920 && int_value <= 2002; + }; + valid_funcs["iyr"] = [](string &value) { + int int_value = stoi(value); + return value.length() == 4 && all_of(value.begin(), value.end(), is_digit) && int_value >= 2010 && int_value <= 2020; + }; + valid_funcs["eyr"] = [](string &value) { + int int_value = stoi(value); + return value.length() == 4 && all_of(value.begin(), value.end(), is_digit) && int_value >= 2020 && int_value <= 2030; + }; + valid_funcs["hgt"] = [](string &value) { + int int_value = stoi(value); + return (value.length() == to_string(int_value).length() + 2) && ( + (value.ends_with("in") && int_value >= 59 && int_value <= 76) || + (value.ends_with("cm") && int_value >= 150 && int_value <= 193) + ); + }; + valid_funcs["hcl"] = [](string &value) { + auto it = value.begin(); + return value.length() == 7 && *it == '#' && all_of(next(it), value.end(), is_hex_digit); + }; + valid_funcs["ecl"] = [](string &value) { + unordered_set valid_eye_colors = { { + "amb"s, "blu"s, "brn"s, "gry"s, "grn"s, "hzl"s, "oth"s + } }; + return valid_eye_colors.contains(value); + }; + valid_funcs["pid"] = [](string &value) { + return value.length() == 9 && all_of(value.begin(), value.end(), is_digit); + }; + + for (string line; getline(input, line); ) { + if (!line.empty()) { // append to existing passport + if (!current_passport.eof()) { + current_passport << ' '; + } + current_passport << line; + } + if (line.empty() || input.peek() == EOF) { // no input; parse current passport + unordered_set mandatory_fields = { + { "byr"s, "iyr"s, "eyr"s, "hgt"s, "hcl"s, "ecl"s, "pid"s} + }; + bool all_fields_valid = true; + for (string entry; getline(current_passport, entry, ' '); ) { + auto field_delim_pos = entry.find(':'); + if (field_delim_pos != string::npos) { + string field_name = entry.substr(0, field_delim_pos); + string field_value = entry.substr(field_delim_pos+1); + mandatory_fields.erase(field_name); + if (valid_funcs.contains(field_name)) { + all_fields_valid = all_fields_valid && valid_funcs[field_name](field_value); + } + } + } + current_passport.clear(); + part1 += (uint64_t) mandatory_fields.empty(); + part2 += (uint64_t) (mandatory_fields.empty() && all_fields_valid); + } + } + return pair(to_string(part1), to_string(part2)); +} diff --git a/c++/solutions.hpp b/c++/solutions.hpp index fdf240c..26b03b8 100644 --- a/c++/solutions.hpp +++ b/c++/solutions.hpp @@ -10,3 +10,4 @@ typedef std::pair (*solution_fn)(std::istream&); DECL_SOLUTION(1) DECL_SOLUTION(2) DECL_SOLUTION(3) +DECL_SOLUTION(4)