diff options
Diffstat (limited to 'src/day19.cpp')
| -rw-r--r-- | src/day19.cpp | 99 |
1 files changed, 37 insertions, 62 deletions
diff --git a/src/day19.cpp b/src/day19.cpp index c991d6d..a1d4eab 100644 --- a/src/day19.cpp +++ b/src/day19.cpp | |||
| @@ -22,7 +22,7 @@ split(string_view line, string_view delimiter) | |||
| 22 | 22 | ||
| 23 | vector<string> res; | 23 | vector<string> res; |
| 24 | 24 | ||
| 25 | while ( (pos_end = line.find(delimiter, pos_start)) != std::string::npos ) { | 25 | while ( (pos_end = line.find(delimiter, pos_start)) != string::npos ) { |
| 26 | auto token = line.substr(pos_start, pos_end - pos_start); | 26 | auto token = line.substr(pos_start, pos_end - pos_start); |
| 27 | pos_start = pos_end + delimiter.length(); | 27 | pos_start = pos_end + delimiter.length(); |
| 28 | 28 | ||
| @@ -36,37 +36,8 @@ split(string_view line, string_view delimiter) | |||
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | void | 38 | void |
| 39 | part1() | 39 | part1(map<string, tuple<vector<tuple<string, string, long, string>>, string>> rules, const vector<string>& parts) |
| 40 | { | 40 | { |
| 41 | const auto input = split(read_file("data/day19.txt"), "\n\n"); | ||
| 42 | const auto rules_string = split(input[0], "\n"); | ||
| 43 | const auto parts = split(input[1], "\n"); | ||
| 44 | |||
| 45 | map<string, tuple<vector<tuple<string, string, long, string>>, string>> rules; | ||
| 46 | |||
| 47 | for ( const auto& rule: rules_string ) { | ||
| 48 | static const regex rules_pattern{ R"((.*)\{(.*),(.*)\})" }; | ||
| 49 | |||
| 50 | smatch smatch; | ||
| 51 | if ( regex_search(rule, smatch, rules_pattern) ) { | ||
| 52 | string rule_name = smatch[1]; | ||
| 53 | string default_dest = smatch[3]; | ||
| 54 | |||
| 55 | vector<tuple<string, string, long, string>> sub_rules; | ||
| 56 | |||
| 57 | for ( const auto& sub_rule: split(smatch[2].str(), ",") ) { | ||
| 58 | static const regex sub_rules_pattern{ R"((.)(.)(\d*):(.*))" }; | ||
| 59 | |||
| 60 | std::smatch smatch2; | ||
| 61 | if ( regex_search(sub_rule, smatch2, sub_rules_pattern) ) { | ||
| 62 | sub_rules.emplace_back(smatch2[1], smatch2[2], stol(smatch2[3]), smatch2[4]); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | rules[rule_name] = make_tuple(sub_rules, default_dest); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | long sum = 0; | 41 | long sum = 0; |
| 71 | for ( const auto& part: parts ) { | 42 | for ( const auto& part: parts ) { |
| 72 | static const regex parts_pattern{ R"(\{x=(\d*),m=(\d*),a=(\d*),s=(\d*)\})" }; | 43 | static const regex parts_pattern{ R"(\{x=(\d*),m=(\d*),a=(\d*),s=(\d*)\})" }; |
| @@ -110,36 +81,9 @@ part1() | |||
| 110 | } | 81 | } |
| 111 | 82 | ||
| 112 | void | 83 | void |
| 113 | part2() | 84 | part2(map<string, tuple<vector<tuple<string, string, long, string>>, string>> rules) |
| 114 | { | 85 | { |
| 115 | const auto input = split(read_file("data/day19.txt"), "\n\n"); | 86 | function<long(map<string, tuple<long, long>>, string)> count = [&](map<string, tuple<long, long>> ranges, const string& name) -> long { |
| 116 | const auto rules_string = split(input[0], "\n"); | ||
| 117 | |||
| 118 | const regex rules_pattern{ R"((.*)\{(.*),(.*)\})" }; | ||
| 119 | const regex sub_rules_pattern{ R"((.)(.)(\d*):(.*))" }; | ||
| 120 | |||
| 121 | map<string, tuple<vector<tuple<string, string, long, string>>, string>> rules; | ||
| 122 | |||
| 123 | for ( const auto& rule: rules_string ) { | ||
| 124 | smatch smatch; | ||
| 125 | if ( regex_search(rule, smatch, rules_pattern) ) { | ||
| 126 | string rule_name = smatch[1]; | ||
| 127 | string default_dest = smatch[3]; | ||
| 128 | |||
| 129 | vector<tuple<string, string, long, string>> sub_rules; | ||
| 130 | |||
| 131 | for ( const auto& sub_rule: split(smatch[2].str(), ",") ) { | ||
| 132 | std::smatch smatch2; | ||
| 133 | if ( regex_search(sub_rule, smatch2, sub_rules_pattern) ) { | ||
| 134 | sub_rules.emplace_back(smatch2[1], smatch2[2], stol(smatch2[3]), smatch2[4]); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | rules[rule_name] = make_tuple(sub_rules, default_dest); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | function<long(map<string, tuple<long, long>>, string)> count = [&](map<string, tuple<long, long>> ranges, string name) -> long { | ||
| 143 | if ( name == "R" ) { | 87 | if ( name == "R" ) { |
| 144 | return 0; | 88 | return 0; |
| 145 | } | 89 | } |
| @@ -203,6 +147,37 @@ part2() | |||
| 203 | int | 147 | int |
| 204 | main() | 148 | main() |
| 205 | { | 149 | { |
| 206 | part1(); | 150 | const auto input = split(read_file("data/day19.txt"), "\n\n"); |
| 207 | part2(); | 151 | const auto rules_string = split(input[0], "\n"); |
| 152 | const auto parts = split(input[1], "\n"); | ||
| 153 | |||
| 154 | map<string, tuple<vector<tuple<string, string, long, string>>, string>> rules; | ||
| 155 | |||
| 156 | for ( const auto& rule: rules_string ) { | ||
| 157 | static const regex rules_pattern{ R"((.*)\{(.*),(.*)\})" }; | ||
| 158 | |||
| 159 | smatch smatch; | ||
| 160 | if ( !regex_search(rule, smatch, rules_pattern) ) { | ||
| 161 | continue; | ||
| 162 | } | ||
| 163 | |||
| 164 | string name = smatch[1]; | ||
| 165 | string default_rule = smatch[3]; | ||
| 166 | |||
| 167 | vector<tuple<string, string, long, string>> sub_rules; | ||
| 168 | |||
| 169 | for ( const auto& sub_rule: split(smatch[2].str(), ",") ) { | ||
| 170 | static const regex sub_rules_pattern{ R"((.)(.)(\d*):(.*))" }; | ||
| 171 | |||
| 172 | std::smatch smatch2; | ||
| 173 | if ( regex_search(sub_rule, smatch2, sub_rules_pattern) ) { | ||
| 174 | sub_rules.emplace_back(smatch2[1], smatch2[2], stol(smatch2[3]), smatch2[4]); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | rules[name] = make_tuple(sub_rules, default_rule); | ||
| 179 | } | ||
| 180 | |||
| 181 | part1(rules, parts); | ||
| 182 | part2(rules); | ||
| 208 | } | 183 | } |
