From 6f75463d197bf7802cca4059e404ed98b1c12062 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Thu, 14 Nov 2024 22:40:26 +0100 Subject: aoc 2015, day 11 --- 2015/src/day11.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 2015/src/day11.cpp (limited to '2015/src/day11.cpp') diff --git a/2015/src/day11.cpp b/2015/src/day11.cpp new file mode 100644 index 0000000..06fd05b --- /dev/null +++ b/2015/src/day11.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +using namespace std; + +void +increment(string& str) +{ + for ( auto it = rbegin(str); it != rend(str); ++it ) { + *it += 1; + if ( *it > 'z' ) { + *it = 'a'; + continue; + } + return; + } +} + +bool +has_increasing_chars(string_view str) +{ + for ( size_t idx = 2; idx != str.length(); ++idx ) { + if ( str[idx - 2] + 1 == str[idx - 1] && str[idx - 1] + 1 == str[idx] ) { + return true; + } + } + return false; +} + +bool +contains_no_invalid_char(const string& str) +{ + return str.find_first_of("iol") == string::npos; +} + +bool +contains_two_pairs(const string& str) +{ + static const regex regex("(.)\\1.*(.)\\2"); + + return regex_search(str, regex); +} + +void +part1(string& str) +{ + while ( true ) { + if ( has_increasing_chars(str) && contains_no_invalid_char(str) && contains_two_pairs(str) ) { + cout << str << endl; + break; + } + increment(str); + } +} + +int +main() +{ + string str = "hepxcrrq"; + + part1(str); + + increment(str); + part1(str); +} -- cgit v1.3