Cześć. taki mam kodzik i takie z nim problemy
Validator to po prostu struct:
template <typename F>
struct validator{
std::regex reg;
F isValid;
};
Robię 4 część challengu adventofcode (https://adventofcode.com/2020/day/4). Generalnie już ją zrobiłem, innym kodem, ale wychodzę z założenia, że jeśli mam zadanie i mam z czymś problem, i nie znam rozwiązania, to nawet jeśli napiszę inny kod rozwiązujacy te zadanie, tak czy siak powinienem rozwiązanie problemu.
Jest to druga część tego wyzwania, która brzmi tak:
The line is moving more quickly now, but you overhear airport security talking about how passports with invalid data are getting through. Better add some data validation, quick!
You can continue to ignore the cid field, but each other field has strict rules about what values are valid for automatic validation:
byr (Birth Year) - four digits; at least 1920 and at most 2002. iyr (Issue Year) - four digits; at least 2010 and at most 2020. eyr (Expiration Year) - four digits; at least 2020 and at most 2030. hgt (Height) - a number followed by either cm or in: If cm, the number must be at least 150 a nd at most 193. If in, the number must be at least 59 and at most 76. hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f. ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth. pid (Passport ID) - a nine-digit number, including leading zeroes. cid (Country ID) - ignored, missing or not.
Your job is to count the passports where all required fields are both present and valid according to the above rules. Here are some example values:
byr valid: 2002
byr invalid: 2003hgt valid: 60in
hgt valid: 190cm
hgt invalid: 190in
hgt invalid: 190hcl valid: #123abc
hcl invalid: #123abz
hcl invalid: 123abcecl valid: brn
ecl invalid: watpid valid: 000000001
pid invalid: 0123456789Here are some invalid passports:
eyr:1972 cid:100
hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926iyr:2019
hcl:#602927 eyr:1967 hgt:170cm
ecl:grn pid:012533040 byr:1946hcl:dab227 iyr:2012
ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277hgt:59cm ecl:zzz
eyr:2038 hcl:74454a iyr:2023
pid:3556412378 byr:2007Here are some valid passports:
pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980
hcl:#623a2feyr:2029 ecl:blu cid:129 byr:1989
iyr:2014 pid:896056539 hcl:#a97842 hgt:165cmhcl:#888785
hgt:164cm byr:2001 iyr:2015 cid:88
pid:545766238 ecl:hzl
eyr:2022iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719
Count the number of valid passports - those that have all required fields and valid values. Continue to treat cid as optional. In your batch file, how many passports are valid?
Plan jest taki:
Tworzę mapę nazwa atrybutu -> walidator (regex + właściwy walidator w postaci funkcji)
Sprawdzam czy każdy atrybut zgadza się z formatem
Jeśli się zgadza, to poddaję go walidacji przypisaną funkcją (3 funkcje zwracają true, ponieważ atrybut jest poprawny już jeśli pasuje do formatu, pozostałe sprawdzają "argumenty" atrybutu)
Zależnie od wyników zwracam true/false
Jednak jest coś nie tak z tym bindem i nie do końca wiem co z tym zrobić. VSC pokazuje mi dwa błędy:
- przy mapie
brak odpowiedniej konwersji elementu "std::_Binder<std::_Unforced, type &, int, int>" na "std::_Binder<std::_Unforced, type &, const std::_Ph<1> &, const std::_Ph<2> &>" zdefiniowanej przez użytkownikaC/C++(312)
- przy vtorFunc(...):
żadne wystąpienie elementu funkcja przeciążona "std::_Binder<_Ret, _Fx, _Types...>::operator() [gdzie _Ret=std::_Unforced, _Fx=type &, _Types=<const std::_Ph<1> &, const std::_Ph<2> &>]" nie jest zgodne z listą argumentów -- typy argumentów: (std::smatch) -- typ obiektu to: std::_Binder<std::_Unforced, type &, const std::_Ph<1> &, const std::_Ph<2> &>C/C++(304)
I szczerze nie mam pojęcia jak to poprawnie zrobić. Prosiłbym o pomoc.
bool validAttr(const std::string& attr){
using namespace std::literals::string_literals;
using namespace std::placeholders;
auto toUnsigned = [](std::ssub_match match){
unsigned ret{};
auto matchStr = match.str();
auto [ptr, err] = std::from_chars(matchStr.data(), matchStr.data()+matchStr.size(), ret);
if(!(std::errc() == err)) throw std::runtime_error("Cannot convert "+matchStr+" to unsigned integer value.\n");
return ret;
};
auto checkBounds = [](unsigned num, unsigned low, unsigned high){
return low <= num && num <= high;
};
auto boundChecker = [&checkBounds, &toUnsigned](const std::smatch& sm, unsigned low, unsigned high){
return checkBounds(toUnsigned(sm[1]), low, high);
};
auto retTrue = [](){ return true; };
auto heightValidator = [&checkBounds, &toUnsigned](const std::smatch& sm){
if("cm" == sm[2]) return checkBounds(toUnsigned(sm[1]), 150, 193);
else if("in" == sm[2]) return checkBounds(toUnsigned(sm[1]), 59, 76);
else return false;
};
using boundValidator_t = decltype(std::bind(boundChecker, _1, _2));
using defValidator_t = decltype(retTrue);
using heightValidator_t = decltype(heightValidator);
using validator_v = std::variant<validator<boundValidator_t>, validator<defValidator_t>, validator<heightValidator_t>>;
std::unordered_map<std::string, validator_v> validators{
{"byr"s, validator<boundValidator_t>{std::regex{"byr:(\\d{4})"}, std::bind(boundChecker, 1920, 2002)}},
{"iyr"s, validator<boundValidator_t>{std::regex{"iyr:(\\d{4})"}, std::bind(boundChecker, 2010, 2020)}},
{"eyr"s, validator<boundValidator_t>{std::regex{"eyr:(\\d{4})"}, std::bind(boundChecker, 2020, 2030)}},
{"hgt"s, validator<heightValidator_t>{std::regex{"hgt:(\\d{2,3})(in|cm)"}, heightValidator}},
{"hcl"s, validator<defValidator_t>{std::regex{"hcl:#([0-9a-f]{6})"}, retTrue}},
{"ecl"s, validator<defValidator_t>{std::regex{"ecl:#(amb|blu|brn|gry|grn|hzl|oth)"}, retTrue}},
{"pid"s, validator<defValidator_t>{std::regex{"pid:\\d{9}"}, retTrue}}
};
auto attrName = attr.substr(0, 3);
auto returnVisitor = [](const auto& validator){return validator};
auto reg = std::visit(returnVisitor, validators[attrName]).reg;
auto vtorFunc = std::visit(returnVisitor, validators[attrName]).isValid;
std::smatch match;
if(std::regex_match(attr, match, reg)){
if(vtorFunc(match)) return true;
return false;
}else return false;
}