From 8c96639ec1f6757570510fc27f1c5fabece35eaf Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 16 Nov 2025 13:23:34 +0100 Subject: aoc 2018, days 1-11 --- 2018/src/day05.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 2018/src/day05.cpp (limited to '2018/src/day05.cpp') diff --git a/2018/src/day05.cpp b/2018/src/day05.cpp new file mode 100644 index 0000000..9de484d --- /dev/null +++ b/2018/src/day05.cpp @@ -0,0 +1,101 @@ +#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'; + */ +} -- cgit v1.3