#include #include void part1(std::ifstream& input, std::string& line) { int valid_count = 0; while (std::getline(input, line)) { size_t fp_i = line.find(' '); // Index of the first space in line size_t dash_i = line.find('-'); // Index of the only dash in line std::string password = line.substr(line.find(": ") + 2); char letter = line[fp_i + 1]; int low = std::stoi(line.substr(0, dash_i)); int high = std::stoi(line.substr(dash_i + 1, fp_i)); int letter_count = std::count(password.cbegin(), password.cend(), letter); if (letter_count <= high && letter_count >= low) valid_count++; } std::cout << valid_count << std::endl; } void part2(std::ifstream& input, std::string& line) { int valid_count = 0; while(std::getline(input, line)) { size_t fp_i = line.find(' '); // Index of the first space in line size_t dash_i = line.find('-'); // Index of the only dash in line std::string password = line.substr(line.find(": ") + 2); char letter = line[fp_i + 1]; size_t i1 = std::stoi(line.substr(0, dash_i)) - 1; size_t i2 = std::stoi(line.substr(dash_i + 1, fp_i)) - 1; if (password[i1] == password[i2]) continue; // If they are the same we can skip this line if (password[i1] == letter || password[i2] == letter) valid_count++; } std::cout << valid_count << std::endl; } int main() { std::ifstream input("../input.txt"); std::string line; part1(input, line); input.clear(); // clear EOF flag input.seekg(0); part2(input, line); return 0; }