From 43e03c742bd15f9e081cdcdca1ba4c3523ebdfbc Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 6 Dec 2019 08:37:15 +0100 Subject: [PATCH] Implement day 06. I forgot how slow std::regex was to compile. --- 2019/src/day06.cpp | 78 ++++++++++++++++++++++++++++++----- 2019/tests/samples/06-2-1.in | 13 ++++++ 2019/tests/samples/06-2-1.out | 1 + 3 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 2019/tests/samples/06-2-1.in create mode 100644 2019/tests/samples/06-2-1.out diff --git a/2019/src/day06.cpp b/2019/src/day06.cpp index 11e4df6..382ed8d 100644 --- a/2019/src/day06.cpp +++ b/2019/src/day06.cpp @@ -1,23 +1,81 @@ #include #include +#include +#include #include "days.hpp" -void aoc2019::day06_part1(std::istream &input, std::ostream &output) { - std::string buffer; - std::regex regex("^([A-Z0-9]+)\\)([A-Z0-9]+)$"); +namespace { + std::vector> read_orbits(std::istream &input) { + std::vector> result; + std::string buffer; - while (std::getline(input, buffer)) { + std::regex regex("^([A-Z0-9]+)\\)([A-Z0-9]+)$"); std::smatch match_results; - if (!std::regex_match(buffer, match_results, regex)) { - std::string error = "Invalid line: "; - error += buffer; - throw std::domain_error(error); + 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]); + } + + return result; + } +} + +void aoc2019::day06_part1(std::istream &input, std::ostream &output) { + std::unordered_map> orbits; + + for (auto[a, b] : read_orbits(input)) { + orbits[std::move(a)].emplace_back(std::move(b)); + } + + std::deque> todo = {{"COM", 0}}; + int total_orbits = 0; + + while (!todo.empty()) { + auto[name, offset] = todo.front(); + todo.pop_front(); + + total_orbits += offset; + + for (const auto& partner : orbits[name]) { + todo.emplace_back(partner, offset + 1); } } - output << "Not implemented\n"; + + output << total_orbits << std::endl; } void aoc2019::day06_part2(std::istream &input, std::ostream &output) { - output << "Not implemented\n"; + std::unordered_map> orbits; + + for (auto[a, b] : read_orbits(input)) { + orbits[a].emplace_back(b); + orbits[b].emplace_back(a); + } + + std::deque> todo = {{"YOU", 0}}; + std::unordered_set visited = { "YOU" }; + + while (!todo.empty()) { + auto[name, offset] = todo.front(); + todo.pop_front(); + + for (const auto& partner : orbits[name]) { + if (partner == "SAN") { + output << offset - 1 << std::endl; + return; + } + if (!visited.count(partner)) { + todo.emplace_back(partner, offset + 1); + visited.emplace(partner); + } + } + } + + throw std::domain_error("No valid path."); } diff --git a/2019/tests/samples/06-2-1.in b/2019/tests/samples/06-2-1.in new file mode 100644 index 0000000..a1007c6 --- /dev/null +++ b/2019/tests/samples/06-2-1.in @@ -0,0 +1,13 @@ +COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L +K)YOU +I)SAN diff --git a/2019/tests/samples/06-2-1.out b/2019/tests/samples/06-2-1.out new file mode 100644 index 0000000..b8626c4 --- /dev/null +++ b/2019/tests/samples/06-2-1.out @@ -0,0 +1 @@ +4