diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-19 20:44:58 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-19 20:44:58 +0100 |
| commit | f52cf2888ddc30c4dc181b88d2a7077ac36f098f (patch) | |
| tree | ec60892dd06286b660ee4684c68501fbb964f091 /2015 | |
| parent | 0413a04a422e5985ae91a02489cafcb85515ea9f (diff) | |
| download | advent-of-code-f52cf2888ddc30c4dc181b88d2a7077ac36f098f.tar.gz advent-of-code-f52cf2888ddc30c4dc181b88d2a7077ac36f098f.tar.bz2 advent-of-code-f52cf2888ddc30c4dc181b88d2a7077ac36f098f.zip | |
aoc 2015, day 19
Diffstat (limited to '2015')
| -rw-r--r-- | 2015/src/day19.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/2015/src/day19.cpp b/2015/src/day19.cpp new file mode 100644 index 0000000..7801964 --- /dev/null +++ b/2015/src/day19.cpp | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <cstddef> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <map> | ||
| 6 | #include <set> | ||
| 7 | #include <sstream> | ||
| 8 | #include <string> | ||
| 9 | #include <tuple> | ||
| 10 | #include <vector> | ||
| 11 | using namespace std; | ||
| 12 | |||
| 13 | auto | ||
| 14 | split(const string& line, char sep = ' ') | ||
| 15 | { | ||
| 16 | vector<string> parts; | ||
| 17 | stringstream input{ line }; | ||
| 18 | |||
| 19 | for ( string part; getline(input, part, sep); ) { | ||
| 20 | parts.emplace_back(part); | ||
| 21 | } | ||
| 22 | |||
| 23 | return parts; | ||
| 24 | } | ||
| 25 | |||
| 26 | tuple<string, vector<tuple<string, string>>> | ||
| 27 | read_file(string_view filename) | ||
| 28 | { | ||
| 29 | fstream input{ filename }; | ||
| 30 | vector<tuple<string, string>> table; | ||
| 31 | string molecule; | ||
| 32 | |||
| 33 | for ( string line; getline(input, line); ) { | ||
| 34 | auto parts = split(line); | ||
| 35 | |||
| 36 | if ( parts.size() == 3 && parts[1] == "=>" ) { | ||
| 37 | table.emplace_back(parts[0], parts[2]); | ||
| 38 | } | ||
| 39 | |||
| 40 | if ( parts.size() == 1 ) { | ||
| 41 | molecule = parts[0]; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | return { molecule, table }; | ||
| 45 | } | ||
| 46 | |||
| 47 | void | ||
| 48 | part1(const string& molecule, const vector<tuple<string, string>>& table) | ||
| 49 | { | ||
| 50 | set<string> set; | ||
| 51 | for ( const auto& [left, right]: table ) { | ||
| 52 | size_t pos = 0; | ||
| 53 | while ( true ) { | ||
| 54 | string temp = molecule; | ||
| 55 | pos = temp.find(left, pos); | ||
| 56 | if ( pos == string::npos ) { | ||
| 57 | break; | ||
| 58 | } | ||
| 59 | temp.replace(pos, left.length(), right); | ||
| 60 | ++pos; | ||
| 61 | |||
| 62 | set.insert(temp); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | cout << set.size() << endl; | ||
| 66 | } | ||
| 67 | |||
| 68 | size_t | ||
| 69 | replace_all(string& str, string_view replace, string_view replaceWith) | ||
| 70 | { | ||
| 71 | size_t count = 0; | ||
| 72 | auto pos = str.find(replace); | ||
| 73 | |||
| 74 | while ( pos != string::npos ) { | ||
| 75 | str.replace(pos, replace.size(), replaceWith); | ||
| 76 | ++count; | ||
| 77 | pos = str.find(replace, pos + replace.size()); | ||
| 78 | } | ||
| 79 | |||
| 80 | return count; | ||
| 81 | } | ||
| 82 | |||
| 83 | void | ||
| 84 | part2(string molecule) | ||
| 85 | { | ||
| 86 | replace_all(molecule, "Rn", "("); | ||
| 87 | replace_all(molecule, "Y", ","); | ||
| 88 | replace_all(molecule, "Ar", ")"); | ||
| 89 | |||
| 90 | molecule.erase(remove_if(molecule.begin(), molecule.end(), [](auto chr) -> bool { | ||
| 91 | return islower(chr); | ||
| 92 | }), | ||
| 93 | molecule.end()); | ||
| 94 | |||
| 95 | transform(molecule.begin(), molecule.end(), molecule.begin(), [](auto chr) { | ||
| 96 | return isalpha(chr) ? 'X' : chr; | ||
| 97 | }); | ||
| 98 | |||
| 99 | size_t count = 0; | ||
| 100 | while ( molecule != "X" ) { | ||
| 101 | for ( const auto& item: { "XX", "X(X)", "X(X,X)", "X(X,X,X)" } ) { | ||
| 102 | count += replace_all(molecule, item, "X"); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | cout << count << endl; | ||
| 107 | } | ||
| 108 | |||
| 109 | int | ||
| 110 | main() | ||
| 111 | { | ||
| 112 | const auto [molecule, table] = read_file("data/day19.txt"); | ||
| 113 | part1(molecule, table); | ||
| 114 | part2(molecule); | ||
| 115 | } | ||
