#include #include #include #include #include #include using namespace std; auto read_file(string_view filename) { fstream input{ filename }; vector> data; for ( string line; getline(input, line); ) { long min = 0; long max = 0; char chr = 0; char password[100]; sscanf(line.data(), "%ld-%ld %c: %99s", &min, &max, &chr, password); // NOLINT data.emplace_back(min, max, chr, password); } return data; } void part1(vector>& data) { long sum = 0; for ( const auto& [min, max, chr, password]: data ) { const auto amount = count(password.begin(), password.end(), chr); if ( min <= amount && amount <= max ) { ++sum; } } cout << sum << endl; } void part2(vector>& data) { long sum = 0; for ( const auto& [min, max, chr, password]: data ) { if ( (password[size_t(min - 1)] == chr) ^ (password[size_t(max - 1)] == chr) ) { ++sum; } } cout << sum << endl; } int main() { auto input = read_file("data/day02.txt"); part1(input); part2(input); }