Advertisement
STANAANDREY

AOCMMXX d4p2

Dec 15th, 2020 (edited)
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using PassportData = map<string, string>;
  4. vector<PassportData> passports;
  5.  
  6. void parse() {
  7.     string line;
  8.     PassportData passportData;
  9.     while (getline(cin, line)) {
  10.         if (line.empty()) {
  11.             passports.push_back(passportData);
  12.             passportData.clear();
  13.             continue;
  14.         }
  15.         basic_istringstream<char> entryStream(line);
  16.         string sToken;
  17.         while (entryStream >> sToken) {
  18.             auto splitIt = find(sToken.begin(), sToken.end(), ':');
  19.             string key = string({sToken.begin(), splitIt});
  20.             string data = string({splitIt + 1, sToken.end()});
  21.             passportData[key] = data;
  22.         }
  23.     }
  24.     passports.push_back(passportData);
  25. }
  26.  
  27. pair<int, string> splitHgt(string hgt) {
  28.     int hgtVal = 0, it = 0;
  29.     while (isdigit(hgt[it])) {
  30.         hgtVal = hgtVal * 10 + int(hgt[it] - '0');
  31.         it++;
  32.     }
  33.     return make_pair(hgtVal, hgt.substr(it));
  34. }
  35.  
  36. bool validateHCL(string hcl) {
  37.     set<char> s = {'#'};
  38.     for (char c = '0'; c <= '9'; c++)
  39.         s.insert(c);
  40.     for (char c = 'a'; c <= 'f'; c++)
  41.         s.insert(c);
  42.     for (char c : hcl)
  43.         if (!s.count(c))
  44.             return false;
  45.     return true;
  46. }
  47.  
  48. bool validatePassportData(PassportData passportData) {
  49.     bool ok = true;
  50.     int byr = atoi(passportData["byr"].c_str());
  51.     ok = ok && (1920 <= byr && byr <= 2002);
  52.     int iyr = atoi(passportData["iyr"].c_str());
  53.     ok = ok && (iyr >= 2010 && iyr <= 2020);
  54.     int eyr = atoi(passportData["eyr"].c_str());
  55.     ok = ok && (2020 <= eyr && eyr <= 2030);
  56.     //hgt
  57.     int hgtVal;
  58.     string hgtUnit;
  59.     tie(hgtVal, hgtUnit) = splitHgt(passportData["hgt"]);
  60.     if (hgtUnit == "cm")
  61.         ok = ok && (150 <= hgtVal && hgtVal <= 193);
  62.     else if (hgtUnit == "in")
  63.         ok = ok && (59 <= hgtVal && hgtVal <= 76);
  64.     else
  65.         ok = false;
  66.     //hcl
  67.     ok = ok && validateHCL(passportData["hcl"]);
  68.     const set<string> ss = {"amb", "blu", "blu", "gry", "grn", "hzl", "oth"};
  69.     ok = ok && ss.count(passportData["ecl"]);
  70.     ok = ok && (passportData["pid"].size() == 9);
  71.     for (char c : passportData["pid"])
  72.         if (!isdigit(c))
  73.             ok = false;
  74.     return ok;
  75. }
  76.  
  77. int main() {
  78.     freopen("text.in", "r", stdin);
  79.     parse();
  80.     cout << count_if(passports.begin(), passports.end(), [](PassportData passportData) {
  81.         map<string, bool> reqKeyDetector {{"byr",false},{"iyr",false},{"eyr",false},{"hgt",false},{"hcl",false},{"ecl",false},{"pid",false}};
  82.         for (auto it : passportData) {
  83.             reqKeyDetector[it.first] = true;
  84.         }
  85.         return accumulate(reqKeyDetector.begin(), reqKeyDetector.end(), true, [](bool acVal, auto p) {
  86.             return acVal && p.second;
  87.         }) && validatePassportData(passportData);//*/
  88.     }) << 'r' << endl;
  89.     return 0;
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement