Add solution for 2016 Day 24

This commit is contained in:
Thraix committed 2026-09-04 19:42:12 +02:00
1 parent edc2fd0b6c
commit 551df7ffb2
4 files changed
+95 -34

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
###########
#0.1.....2#
#.#######.#
#4.......3#
###########
+59 -12
View File
@@ -2,30 +2,77 @@
namespace y2016::day24 namespace y2016::day24
{ {
REGISTER_DAY(2016, Day24, std::vector<int>, int); REGISTER_DAY(2016, Day24, Array2D<char>, int);
REGISTER_TEST_EXAMPLE(2016, Day24, ExampleInput, 1, 0); REGISTER_TEST_EXAMPLE(2016, Day24, ExampleInput, 1, 14);
REGISTER_TEST(2016, Day24, Input, 1, 0); REGISTER_TEST(2016, Day24, Input, 1, 448);
REGISTER_TEST_EXAMPLE(2016, Day24, ExampleInput, 2, 0); REGISTER_TEST_EXAMPLE(2016, Day24, ExampleInput, 2, 20);
REGISTER_TEST(2016, Day24, Input, 2, 0); REGISTER_TEST(2016, Day24, Input, 2, 672);
READ_INPUT(input) READ_INPUT(input)
{ {
std::vector<int> vec; return Input::ReadArray2D(input);
std::string str;
while (getline(input, str))
{
} }
return vec;
struct State
{
Index2D pos;
char goalChar;
bool operator<(const State& other) const
{
return pos < other.pos;
}
};
std::vector<std::pair<int, State>> Branch(const Array2D<char>& map, const State& state)
{
std::vector<std::pair<int, State>> branches;
for (const auto& pos : map.GetNeighbors(state.pos, false))
{
if (map[pos] != '#')
branches.emplace_back(1, State{pos, state.goalChar});
}
return branches;
}
bool Goal(const Array2D<char>& map, const State& state)
{
return map[state.pos] == state.goalChar;
}
Graph<char, int> GetGraph(const Array2D<char>& map)
{
std::vector<Index2D> positions(10);
for (auto it = map.begin(); it != map.end(); it++)
{
if (*it >= '0' && *it <= '9')
{
positions[*it - '0'] = it.index;
}
}
while (!positions.back().IsValid())
positions.pop_back();
Graph<char, int> graph;
for (size_t i = 0; i < positions.size(); i++)
{
for (size_t j = i + 1; j < positions.size(); j++)
{
int distance = Helper::Dijkstras(map, State{positions[i], (char)(j + '0')}, Branch, Goal);
graph.AddTwoWayEdge(i + '0', j + '0', distance);
}
}
return graph;
} }
OUTPUT1(input) OUTPUT1(input)
{ {
return 0; return Helper::TSP(GetGraph(input), '0', false);
} }
OUTPUT2(input) OUTPUT2(input)
{ {
return 0; return Helper::TSP(GetGraph(input), '0', true);
} }
} }
+5
View File
@@ -106,6 +106,11 @@ public:
return graph.find(from)->second.find(to)->second; return graph.find(from)->second.find(to)->second;
} }
const std::map<Key, std::map<Key, Value>>& GetGraph() const
{
return graph;
}
const std::map<Key, Value>& GetPaths(const Key& from) const const std::map<Key, Value>& GetPaths(const Key& from) const
{ {
return graph.find(from)->second; return graph.find(from)->second;
+26 -22
View File
@@ -506,17 +506,29 @@ struct Helper
static void ResetTraceErrors(); static void ResetTraceErrors();
private: private:
template <typename Key, typename Value, typename Compare, typename Eval> template <typename Key, typename Value>
static Value TSPCalculatePath(const Graph<Key, Value>& graph, const std::vector<Key>& path, bool returnToStart)
{
int cost = 0;
for (int i = 0; i < path.size() - 1; i++)
{
cost += graph.GetEdge(path[i], path[i + 1]);
}
if (returnToStart)
cost += graph.GetEdge(path.back(), path.front());
return cost;
}
template <typename Key, typename Value>
static int TSP(const Graph<Key, Value>& graph, static int TSP(const Graph<Key, Value>& graph,
const std::set<Key>& nodesLeft, const std::set<Key>& nodesLeft,
const std::vector<Key>& order, const std::vector<Key>& order,
Compare compare, bool returnToStart)
Eval eval)
{ {
if (nodesLeft.empty()) if (nodesLeft.empty())
{ return TSPCalculatePath(graph, order, returnToStart);
return eval(graph, order);
}
bool hasSet = false; bool hasSet = false;
Value tsp; Value tsp;
@@ -528,9 +540,9 @@ private:
std::vector<Key> newOrder = order; std::vector<Key> newOrder = order;
newOrder.emplace_back(node); newOrder.emplace_back(node);
if (hasSet) if (hasSet)
tsp = compare(tsp, TSP(graph, newNodesLeft, newOrder, compare, eval)); tsp = std::min(tsp, TSP(graph, newNodesLeft, newOrder, returnToStart));
else else
tsp = TSP(graph, newNodesLeft, newOrder, compare, eval); tsp = TSP(graph, newNodesLeft, newOrder, returnToStart);
hasSet = true; hasSet = true;
} }
return tsp; return tsp;
@@ -539,28 +551,20 @@ private:
public: public:
// Key - Node type of the graph // Key - Node type of the graph
// Value - Edge type of the graph // Value - Edge type of the graph
// Compare - Value(Value, Value) template <typename Key, typename Value>
// Given two solutions, which one is better static int TSP(const Graph<Key, Value>& graph, const Key& start, bool returnToStart)
// Eval - int(const Graph&, const std::vector<Key>&)
// Given the graph and order of the TSP, what is the total cost
template <typename Key, typename Value, typename Compare, typename Eval>
static int TSP(const Graph<Key, Value>& graph, const Key& start, Compare compare, Eval eval)
{ {
std::set<Key> nodes = graph.GetNodes(); std::set<Key> nodes = graph.GetNodes();
nodes.erase(nodes.find(start)); nodes.erase(nodes.find(start));
return TSP(graph, nodes, std::vector<Key>{start}, compare, eval); return TSP(graph, nodes, std::vector<Key>{start}, returnToStart);
} }
// Key - Node type of the graph // Key - Node type of the graph
// Value - Edge type of the graph // Value - Edge type of the graph
// Compare - Value(Value, Value) template <typename Key, typename Value>
// Given two solutions, which one is better static int TSP(const Graph<Key, Value>& graph, bool returnToStart)
// Eval - int(const Graph&, const std::vector<Key>&)
// Given the graph and order of the TSP, what is the total cost
template <typename Key, typename Value, typename Compare, typename Eval>
static int TSP(const Graph<Key, Value>& graph, Compare compare, Eval eval)
{ {
return TSP(graph, graph.GetNodes(), std::vector<Key>{}, compare, eval); return TSP(graph, graph.GetNodes(), std::vector<Key>{}, returnToStart);
} }
static bool traceErrors; static bool traceErrors;