From 4a311da6b94dd2252d17901620c8b37b5048c8e2 Mon Sep 17 00:00:00 2001 From: skasch Date: Wed, 27 Dec 2023 14:52:13 +0100 Subject: [PATCH] Optimize d23p2 --- day-23/part-2/skasch.cpp | 44 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/day-23/part-2/skasch.cpp b/day-23/part-2/skasch.cpp index 8ab0b03..87f9f6c 100644 --- a/day-23/part-2/skasch.cpp +++ b/day-23/part-2/skasch.cpp @@ -1,6 +1,6 @@ +#include #include #include -#include #include #include #include @@ -24,7 +24,8 @@ static std::array, kNNodes> kGraph; static std::array kPos = {kStart, kEnd}; static std::unordered_map kIndex = {{kStart, 0}, {kEnd, 1}}; static std::array, kNNodes> kVisitedDirections; -static std::array kVisited; +static std::bitset kVisited; +static std::array kActions; static int kCountNodes = 2; @@ -100,34 +101,35 @@ void BuildGraph(const std::string& input) { } } -int FindLongestPath(const std::string& input) { - std::vector path; - std::size_t total_distance = 0; - std::deque actions = {0}; - std::size_t longest = 0; - while (!actions.empty()) { - int action = actions.front(); - actions.pop_front(); - if (action == -1) { - int prev_pos = path.back(); - kVisited[prev_pos] = false; - path.pop_back(); - total_distance -= kGraph[prev_pos][path.back()]; +int FindLongestPath() { + int total_distance = 0; + int idx = 0; + int longest = 0; + int pos = 0; + while (idx >= 0) { + int action = kActions[idx]; + --idx; + if (action < 0) { + kVisited[pos] = false; + total_distance -= kGraph[pos][~action]; + pos = ~action; continue; } - if (!path.empty()) { - total_distance += kGraph[path.back()][action]; + if (pos != action) { + total_distance += kGraph[pos][action]; } kVisited[action] = true; - path.push_back(action); + pos = action; if (action == 1) { longest = std::max(longest, total_distance); continue; } for (const auto& [next_pos, _] : kGraph[action]) { if (kVisited[next_pos]) continue; - actions.push_front(-1); - actions.push_front(next_pos); + ++idx; + kActions[idx] = ~pos; + ++idx; + kActions[idx] = next_pos; } } return longest; @@ -136,7 +138,7 @@ int FindLongestPath(const std::string& input) { std::string Run(const std::string& input) { // Your code goes here BuildGraph(input); - return std::to_string(FindLongestPath(input)); + return std::to_string(FindLongestPath()); } int main(int argc, char* argv[]) {