From 594c061c603359ef9c5273e19e5eb9e7114a6a68 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 6 Dec 2019 18:25:18 +0100 Subject: [PATCH] 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. --- 2019/src/day06.cpp | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/2019/src/day06.cpp b/2019/src/day06.cpp index 5aa3385..8ea162a 100644 --- a/2019/src/day06.cpp +++ b/2019/src/day06.cpp @@ -1,25 +1,18 @@ +#include #include -#include -#include -#include +#include +#include #include "days.hpp" namespace { std::vector> read_orbits(std::istream &input) { std::vector> result; - std::string buffer; + std::string name1, name2; - std::regex regex("^([A-Z0-9]+)\\)([A-Z0-9]+)$"); - std::smatch match_results; + while (std::getline(input, name1, ')')) { + std::getline(input, name2); - while (std::getline(input, buffer)) { - 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]); + result.emplace_back(name1, name2); } return result;