From 3da0605201572e41500e187258f4995a1d03bbd2 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Mon, 23 Dec 2019 23:48:53 +0100 Subject: [PATCH] Speed up day 18 part 2. --- 2019/src/day18.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/2019/src/day18.cpp b/2019/src/day18.cpp index 1f912f8..e5c8452 100644 --- a/2019/src/day18.cpp +++ b/2019/src/day18.cpp @@ -155,7 +155,7 @@ void aoc2019::day18_part1(std::istream &input, std::ostream &output) { } void aoc2019::day18_part2(std::istream &input, std::ostream &output) { - using state_t = std::tuple>; + using state_t = std::pair>; auto map = read_map(input); @@ -177,7 +177,7 @@ void aoc2019::day18_part2(std::istream &input, std::ostream &output) { const auto implied_graph = compute_implied_graph(map); std::priority_queue, std::vector>, std::greater<>> todo; - std::map visited; + std::map, int> visited; todo.emplace(0, state_t(0, {'@', '*', '^', '/'})); auto target_size = std::count_if(implied_graph.cbegin(), implied_graph.cend(), @@ -188,10 +188,6 @@ void aoc2019::day18_part2(std::istream &input, std::ostream &output) { const auto[dist, state] = todo.top(); todo.pop(); - if (visited[state] < dist) { - continue; - } - auto[keys, pos] = state; if (std::__popcount(keys) == target_size) { @@ -217,8 +213,8 @@ void aoc2019::day18_part2(std::istream &input, std::ostream &output) { next_pos[i] = edge.first; state_t next_state = {next_keys, next_pos}; - if (auto it = visited.find(next_state); it == visited.end() || it->second > next_dist) { - visited[next_state] = next_dist; + if (auto it = visited.find({next_keys, next_pos[i]}); it == visited.end() || it->second > next_dist) { + visited[{next_keys, next_pos[i]}] = next_dist; todo.emplace(next_dist, next_state); } }