diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-14 22:40:26 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-14 22:40:26 +0100 |
| commit | 6f75463d197bf7802cca4059e404ed98b1c12062 (patch) | |
| tree | 3e4d577fdca8005154baedaf7b105ca2245ad7a9 /2015 | |
| parent | 5de5426c8f037b32131b78f3076dc94c83a34508 (diff) | |
| download | advent-of-code-6f75463d197bf7802cca4059e404ed98b1c12062.tar.gz advent-of-code-6f75463d197bf7802cca4059e404ed98b1c12062.tar.bz2 advent-of-code-6f75463d197bf7802cca4059e404ed98b1c12062.zip | |
aoc 2015, day 11
Diffstat (limited to '2015')
| -rw-r--r-- | 2015/src/day11.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
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 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <regex> | ||
| 3 | #include <string> | ||
| 4 | |||
| 5 | using namespace std; | ||
| 6 | |||
| 7 | void | ||
| 8 | increment(string& str) | ||
| 9 | { | ||
| 10 | for ( auto it = rbegin(str); it != rend(str); ++it ) { | ||
| 11 | *it += 1; | ||
| 12 | if ( *it > 'z' ) { | ||
| 13 | *it = 'a'; | ||
| 14 | continue; | ||
| 15 | } | ||
| 16 | return; | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | bool | ||
| 21 | has_increasing_chars(string_view str) | ||
| 22 | { | ||
| 23 | for ( size_t idx = 2; idx != str.length(); ++idx ) { | ||
| 24 | if ( str[idx - 2] + 1 == str[idx - 1] && str[idx - 1] + 1 == str[idx] ) { | ||
| 25 | return true; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | return false; | ||
| 29 | } | ||
| 30 | |||
| 31 | bool | ||
| 32 | contains_no_invalid_char(const string& str) | ||
| 33 | { | ||
| 34 | return str.find_first_of("iol") == string::npos; | ||
| 35 | } | ||
| 36 | |||
| 37 | bool | ||
| 38 | contains_two_pairs(const string& str) | ||
| 39 | { | ||
| 40 | static const regex regex("(.)\\1.*(.)\\2"); | ||
| 41 | |||
| 42 | return regex_search(str, regex); | ||
| 43 | } | ||
| 44 | |||
| 45 | void | ||
| 46 | part1(string& str) | ||
| 47 | { | ||
| 48 | while ( true ) { | ||
| 49 | if ( has_increasing_chars(str) && contains_no_invalid_char(str) && contains_two_pairs(str) ) { | ||
| 50 | cout << str << endl; | ||
| 51 | break; | ||
| 52 | } | ||
| 53 | increment(str); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | int | ||
| 58 | main() | ||
| 59 | { | ||
| 60 | string str = "hepxcrrq"; | ||
| 61 | |||
| 62 | part1(str); | ||
| 63 | |||
| 64 | increment(str); | ||
| 65 | part1(str); | ||
| 66 | } | ||
