#include #include #include #include #include #include #include using namespace std; namespace { vector read_lines(const filesystem::path& filename) { ifstream file{ filename }; vector lines; for ( string line; getline(file, line); ) { lines.emplace_back(line); } return lines; } void part1(const vector& lines) { int twos = 0; int threes = 0; for ( const auto& line: lines ) { map foo; for ( const auto chr: line ) { foo[chr]++; } set counts; for ( const auto& [chr, count]: foo ) { counts.insert(count); } if ( counts.contains(2) ) { ++twos; } if ( counts.contains(3) ) { ++threes; } } cout << "Part 1: " << twos * threes << '\n'; } void part2(const vector& lines) { for ( size_t i = 0; i != lines.size(); ++i ) { for ( size_t j = i + 1; j != lines.size(); ++j ) { const auto& lhs = lines.at(i); const auto& rhs = lines.at(j); if ( lhs.length() != rhs.length() ) { throw runtime_error("invalid data"); } string result; for ( size_t k = 0; k != lhs.length(); ++k ) { if ( lhs[k] == rhs[k] ) { result += lhs[k]; } } if ( result.length() + 1 == lhs.length() ) { cout << "Part 2: " << result << '\n'; return; } } } } } // namespace int main() { auto lines = read_lines("data/day02.txt"); part1(lines); part2(lines); }