Something went wrong. Try again.
advent of code stuff
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149#include <algorithm>#include <iostream>#include <regex>#include <set>#include <sstream>#include <string>#include <unordered_set>#include <vector>
boolis_within_range(int num, int lb, int ub);
class Passport{ std::optional<std::string> byr; std::optional<std::string> iyr; std::optional<std::string> eyr; std::optional<std::string> hgt; std::optional<std::string> hcl; std::optional<std::string> ecl; std::optional<std::string> pid; std::optional<std::string> cid;
public: std::optional<std::string> get_value(std::string key) const; bool is_valid(void) const; void set_value(std::string key, std::string value);};
intmain(void){ std::string line; std::vector<Passport> 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<std::string>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 {};}
voidPassport::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;}
boolPassport::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;}
boolis_within_range(int num, int lb, int ub){ return num >= lb && num <= ub;}