#include #include #include #include using namespace std; namespace { string read_line(const filesystem::path& filename) { ifstream file{ filename }; string line; getline(file, line); return line; } bool xorEqual(char lhs, char rhs) { return (lhs ^ (1 << 5)) == rhs; } #if 0 void part1(string str) { size_t idx = 1; while ( idx < str.length() ) { if ( xorEqual(str[idx - 1], str[idx]) ) { str.erase(idx - 1, 2); idx = 1; } else { ++idx; } } cout << "Part 1: " << str.length() << '\n'; } #endif void part1_opti(string_view str) { string result; for ( const auto chr: str ) { if ( !result.empty() && xorEqual(result.back(), chr) ) { result.pop_back(); } else { result.push_back(chr); } } cout << "Part 1: " << result.length() << '\n'; } void part2(string_view str) { size_t min_length = str.size(); for ( auto ch = 'a'; ch != 'z'; ++ch ) { string result; for ( const auto chr: str ) { if ( tolower(chr) == ch ) { // nichts } else if ( !result.empty() && xorEqual(result.back(), chr) ) { result.pop_back(); } else { result.push_back(chr); } } min_length = min(min_length, result.length()); } cout << "Part 2: " << min_length << '\n'; } } // namespace int main() { auto line = read_line("data/day05.txt"); // part1(line); part1_opti(line); part2(line); /* cout << reduce("aA") << '\n'; cout << reduce("abBA") << '\n'; cout << reduce("abAB") << '\n'; cout << reduce("aabAAB") << '\n'; cout << reduce("dabAcCaCBAcCcaDA") << '\n'; */ }