Skip to content

Instantly share code, notes, and snippets.

@m42e
Created December 5, 2020 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m42e/1cb25e6117fbb2754505f75376fa6683 to your computer and use it in GitHub Desktop.
Save m42e/1cb25e6117fbb2754505f75376fa6683 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string line;
ifstream myfile("Text.txt");
//byr iyr eye hgt hcl ecl pid cid
/*byr(Birth Year)
iyr(Issue Year)
eyr(Expiration Year)
hgt(Height)
hcl(Hair Color)
ecl(Eye Color)
pid(Passport ID)
cid(Country ID)*/
int valid = 0;
int valid2 = 0;
if (myfile.is_open())
{
string passport;
while (getline(myfile, line))
{
passport += line + " ";
if (line == "") {
bool byr, iyr, eyr, hgt, hcl, ecl, pid;
byr = iyr = eyr = hgt = hcl = ecl = pid = false;
if (passport.find("byr:") != std::string::npos) {
byr = true;
}
if (passport.find("iyr:") != std::string::npos) {
iyr = true;
}
if (passport.find("eyr:") != std::string::npos) {
eyr = true;
}
if (passport.find("hgt:") != std::string::npos) {
hgt = true;
}
if (passport.find("hcl:") != std::string::npos) {
hcl = true;
}
if (passport.find("ecl:") != std::string::npos) {
ecl = true;
}
if (passport.find("pid:") != std::string::npos) {
pid = true;
}
if (byr == iyr == eyr == hgt == hcl == ecl == pid == true) {
valid++;
}
if (byr == true && iyr == true && eyr == true && hgt == true && hcl == true && ecl == true && pid == true) {
valid2++;
}
passport = "";
}
}
}
cout << valid;
cout << valid2;
myfile.close();
}
@m42e
Copy link
Author

m42e commented Dec 5, 2020

or with some output:

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm> 

using namespace std; 
int main() {

	string line; 
	ifstream myfile("Text.txt");
	//byr iyr eye hgt hcl ecl pid cid
		/*byr(Birth Year)
		iyr(Issue Year)
		eyr(Expiration Year)
		hgt(Height)
		hcl(Hair Color)
		ecl(Eye Color)
		pid(Passport ID)
		cid(Country ID)*/
	
	int valid = 0; 
	int valid2 = 0; 
	if (myfile.is_open())
	{
		string passport;
		while (getline(myfile, line))
		{
			passport += line + " "; 
			if (line == "") {

				bool byr, iyr, eyr, hgt, hcl, ecl, pid;
				byr = iyr = eyr = hgt = hcl = ecl = pid = false; 
				if (passport.find("byr:") != std::string::npos) {
					 byr = true;
				}

				if (passport.find("iyr:") != std::string::npos) {
					 iyr = true;
				}

				if (passport.find("eyr:") != std::string::npos) {
					 eyr = true;
				}

				if (passport.find("hgt:") != std::string::npos) {
					 hgt = true;
				}

				if (passport.find("hcl:") != std::string::npos) {
					 hcl = true;
				}

				if (passport.find("ecl:") != std::string::npos) {
					 ecl = true;
				}

				if (passport.find("pid:") != std::string::npos) {
					 pid = true;
				}

        bool a, b;
        a = false;
        b = false;
				if (byr == iyr == eyr == hgt == hcl == ecl == pid == true) {

					valid++;
          a = true;
				}
				if (byr == true && iyr == true && eyr == true && hgt == true && hcl == true && ecl == true && pid == true) {

					valid2++;
          b=true;
				}

        if (a != b){
          cout << (pid == true) << endl;
          cout << (ecl == pid == true) << endl;
          cout << (hcl == ecl == pid == true) << endl;
          cout << (hgt == hcl == ecl == pid == true) << endl;
          cout << (eyr == hgt == hcl == ecl == pid == true) << endl;
          cout << (iyr == eyr == hgt == hcl == ecl == pid == true) << endl;
          cout << (byr == iyr == eyr == hgt == hcl == ecl == pid == true) << endl;
          cout << passport;
        }

				passport = "";
			}
		}
	}
	cout << valid; 
	cout << valid2; 
	myfile.close();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment