Replace regex with simpler string parsing.

Remove the dependency on std::regex which is slow to both compile and
run. Saves about half the execution time.
This commit is contained in:
2019-12-06 18:25:18 +01:00
parent 5c9dc0b6bd
commit 594c061c60

View File

@@ -1,25 +1,18 @@
#include <deque>
#include <iostream> #include <iostream>
#include <regex> #include <unordered_map>
#include <queue> #include <vector>
#include <unordered_set>
#include "days.hpp" #include "days.hpp"
namespace { namespace {
std::vector<std::pair<std::string, std::string>> read_orbits(std::istream &input) { std::vector<std::pair<std::string, std::string>> read_orbits(std::istream &input) {
std::vector<std::pair<std::string, std::string>> result; std::vector<std::pair<std::string, std::string>> result;
std::string buffer; std::string name1, name2;
std::regex regex("^([A-Z0-9]+)\\)([A-Z0-9]+)$"); while (std::getline(input, name1, ')')) {
std::smatch match_results; std::getline(input, name2);
while (std::getline(input, buffer)) { result.emplace_back(name1, name2);
if (!std::regex_match(buffer, match_results, regex)) {
std::string error = "Invalid line: ";
error += buffer;
throw std::domain_error(error);
}
result.emplace_back(match_results[1], match_results[2]);
} }
return result; return result;