r/adventofcode • • Dec 22 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 22 Solutions -🎄-

--- Day 22: Mode Maze ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 22

Transcript:

Upping the Ante challenge: complete today's puzzles using ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 01:02:36!

12 Upvotes

102 comments sorted by

View all comments

Show parent comment

1

u/wlandry Dec 22 '18

I figured out how to get astar_search to work. It is not any faster, since the expensive part is setting up the cave, not searching it. In any event, to use astar_search change the beginning to

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/astar_search.hpp>

#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <vector>

using EdgeWeightProperty = boost::property<boost::edge_weight_t, int64_t>;
using Graph
  = boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS,
                          boost::no_property, EdgeWeightProperty>;

// euclidean distance heuristic
template <class Graph, class CostType>
class distance_heuristic : public boost::astar_heuristic<Graph, CostType>
{
public:
  using Vertex = typename boost::graph_traits<Graph>::vertex_descriptor;
  distance_heuristic(const Vertex &Goal, const int64_t &Max_x)
      : goal(Goal), max_x(Max_x)
  {}
  CostType operator()(Vertex u)
  {
    CostType dtool = std::abs(int64_t(goal % 3 - u % 3));
    CostType dx = std::abs(int64_t((goal / 3) % max_x - (u / 3) % max_x));
    CostType dy = std::abs(int64_t((goal / 3) / max_x - (u / 3) / max_x));
    const int64_t tool_cost(7), move_cost(1);
    return dtool * tool_cost + move_cost*(dx + dy);
  }
  Vertex goal;
  size_t max_x;
};

and then replace the call to dijkstra_shortest_paths with

boost::astar_search(g, vertex(index(0, 0, 1, max_x), g),
                  distance_heuristic<Graph, int64_t>(
                    vertex(index(target_x, target_y, 1, max_x), g), max_x),
                  boost::distance_map(d.data()));