#include #include #include #include #include #include #include #include bool is_within_range(int num, int lb, int ub); class Passport { std::optional byr; std::optional iyr; std::optional eyr; std::optional hgt; std::optional hcl; std::optional ecl; std::optional pid; std::optional cid; public: std::optional get_value(std::string key) const; bool is_valid(void) const; void set_value(std::string key, std::string value); }; int main(void) { std::string line; std::vector passports{ {} }; while (std::getline(std::cin, line)) { if (line.empty()) { passports.emplace_back(); } else { std::stringstream field_stream(line); std::string field; while (std::getline(field_stream, field, ' ')) { size_t sep_idx = field.find(':'); std::string key = field.substr(0, sep_idx); std::string value = field.substr(sep_idx + 1); passports.back().set_value(key, value); } } } size_t valid_count = std::count_if(passports.begin(), passports.end(), [](const Passport& p) { return p.is_valid(); }); std::cout << valid_count << std::endl; return EXIT_SUCCESS; } std::optional Passport::get_value(std::string key) const { // clang-format off if (key == "byr") { return this->byr; }; if (key == "iyr") { return this->iyr; }; if (key == "eyr") { return this->eyr; }; if (key == "hgt") { return this->hgt; }; if (key == "hcl") { return this->hcl; }; if (key == "ecl") { return this->ecl; }; if (key == "pid") { return this->pid; }; if (key == "cid") { return this->cid; }; // clang-format on std::cerr << "what the fuck" << std::endl; return {}; } void Passport::set_value(std::string key, std::string value) { // clang-format off if (key == "byr") { this->byr = value; return; }; if (key == "iyr") { this->iyr = value; return; }; if (key == "eyr") { this->eyr = value; return; }; if (key == "hgt") { this->hgt = value; return; }; if (key == "hcl") { this->hcl = value; return; }; if (key == "ecl") { this->ecl = value; return; }; if (key == "pid") { this->pid = value; return; }; if (key == "cid") { this->cid = value; return; }; // clang-format on std::cerr << "what the fuck (" << key << ")" << std::endl; } bool Passport::is_valid(void) const { // clang-format off if (!this->byr) { return false; }; if (!this->iyr) { return false; }; if (!this->eyr) { return false; }; if (!this->hgt) { return false; }; if (!this->hcl) { return false; }; if (!this->ecl) { return false; }; if (!this->pid) { return false; }; // clang-format on // clang-format off if (!is_within_range(std::stoi(*this->byr), 1920, 2002)) { return false; } if (!is_within_range(std::stoi(*this->iyr), 2010, 2020)) { return false; } if (!is_within_range(std::stoi(*this->eyr), 2020, 2030)) { return false; } // clang-format on const std::string& h = this->hgt.value(); int height_num = std::stoi(h.substr(0, h.length() - 2)); if (h.substr(h.length() - 2, h.length() - 1) == "cm") { if (!is_within_range(height_num, 150, 193)) return false; } else if (h.substr(h.length() - 2, h.length() - 1) == "in") { if (!is_within_range(height_num, 59, 76)) return false; } else { return false; } if (!std::regex_match(this->hcl.value(), std::regex("#[0-9a-f]{6}"))) { return false; } if (!std::regex_match(this->ecl.value(), std::regex("amb|blu|brn|gry|grn|hzl|oth"))) { return false; } if (!std::regex_match(this->pid.value(), std::regex("[0-9]{9}"))) { return false; } return true; } bool is_within_range(int num, int lb, int ub) { return num >= lb && num <= ub; }