Refactor Graph class into a template to remove LabelGraph and WeightGraph
This commit is contained in:
parent
8e3ca71bda
commit
b71d904770
@ -15,7 +15,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <aoc/common/LabelGraph.hpp>
|
#include <map>
|
||||||
|
|
||||||
|
#include <aoc/common/Graph.hpp>
|
||||||
#include <aoc/framework/Solver-types.hpp>
|
#include <aoc/framework/Solver-types.hpp>
|
||||||
|
|
||||||
class LanParty
|
class LanParty
|
||||||
@ -29,7 +31,8 @@ class LanParty
|
|||||||
private:
|
private:
|
||||||
static constexpr std::string getFirstTxComputerName();
|
static constexpr std::string getFirstTxComputerName();
|
||||||
static constexpr std::string getLastTxComputerName();
|
static constexpr std::string getLastTxComputerName();
|
||||||
LabelGraph lan_;
|
Graph<std::string, void> lan_{};
|
||||||
|
std::map<std::string, int> labelMap_{};
|
||||||
int findOrAddVertex(const std::string& vertexId);
|
int findOrAddVertex(const std::string& vertexId);
|
||||||
void computeInterconnectedThreeSetCount(const int vertexTx);
|
void computeInterconnectedThreeSetCount(const int vertexTx);
|
||||||
bool canProcessVertex(const int vertexToCheck, const int vertexTx) const;
|
bool canProcessVertex(const int vertexToCheck, const int vertexTx) const;
|
||||||
|
@ -15,9 +15,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <aoc/common/GraphPathsResult.hpp>
|
#include <functional>
|
||||||
|
|
||||||
|
#include <aoc/common/Dijkstra.hpp>
|
||||||
|
#include <aoc/common/Graph.hpp>
|
||||||
#include <aoc/common/Grid.hpp>
|
#include <aoc/common/Grid.hpp>
|
||||||
#include <aoc/common/WeightGraph.hpp>
|
|
||||||
#include <aoc/framework/Solver-types.hpp>
|
#include <aoc/framework/Solver-types.hpp>
|
||||||
|
|
||||||
class RamRun
|
class RamRun
|
||||||
@ -34,11 +36,12 @@ class RamRun
|
|||||||
int maxBytes_;
|
int maxBytes_;
|
||||||
int nBytes_;
|
int nBytes_;
|
||||||
Grid<int> vertexReferences_;
|
Grid<int> vertexReferences_;
|
||||||
GraphPathsResult dijkstraResult_;
|
std::function<int(int)> edgeWeightFunctor_;
|
||||||
|
Dijkstra::PathsResult dijkstraResult_;
|
||||||
static constexpr char getUnknownVertexReference();
|
static constexpr char getUnknownVertexReference();
|
||||||
static constexpr char getNoVertexReference();
|
static constexpr char getNoVertexReference();
|
||||||
bool tryMarkCorrupted(const std::string& line, int& corruptedVertex);
|
bool tryMarkCorrupted(const std::string& line, int& corruptedVertex);
|
||||||
WeightGraph buildGraph();
|
Graph<void, void> buildGraph();
|
||||||
int getVertex(WeightGraph& graph, const size_t x, const size_t y);
|
int getVertex(Graph<void, void>& graph, const size_t x, const size_t y);
|
||||||
void resetVertexReferences();
|
void resetVertexReferences();
|
||||||
};
|
};
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
#include <set>
|
#include <set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <aoc/common/WeightGraph.hpp>
|
#include <aoc/common/Graph.hpp>
|
||||||
#include <aoc/extra/ReindeerMazeCrossing.hpp>
|
#include <aoc/extra/ReindeerMazeCrossing.hpp>
|
||||||
#include <aoc/extra/ReindeerMazePathIncidence.hpp>
|
#include <aoc/extra/ReindeerMazePathIncidence.hpp>
|
||||||
#include <aoc/framework/LinesSolver.hpp>
|
#include <aoc/framework/LinesSolver.hpp>
|
||||||
@ -39,15 +39,15 @@ class ReindeerMaze
|
|||||||
static constexpr char getEndChar();
|
static constexpr char getEndChar();
|
||||||
static constexpr char getWallChar();
|
static constexpr char getWallChar();
|
||||||
static constexpr int getTurnCost();
|
static constexpr int getTurnCost();
|
||||||
void buildPathSegmentGraph(WeightGraph& graph, VertexAttachedPositions& vertexAttachedPositions,
|
void buildPathSegmentGraph(Graph<int, int>& graph, VertexAttachedPositions& vertexAttachedPositions,
|
||||||
const int entry, const int exit);
|
const int entry, const int exit);
|
||||||
void initializeWorkList(std::list<ReindeerMazeCrossing>& crossings, const int entryVertex);
|
void initializeWorkList(std::list<ReindeerMazeCrossing>& crossings, const int entryVertex);
|
||||||
void addCheckedIncidence(std::vector<ReindeerMazePathIncidence>& incidences, const Point2 start,
|
void addCheckedIncidence(std::vector<ReindeerMazePathIncidence>& incidences, const Point2 start,
|
||||||
const Point2 direction);
|
const Point2 direction);
|
||||||
void addPathSegmentEdges(WeightGraph& graph, const ReindeerMazePathIncidence& pathIncidence,
|
void addPathSegmentEdges(Graph<int, int>& graph, const ReindeerMazePathIncidence& pathIncidence,
|
||||||
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences);
|
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences);
|
||||||
std::pair<int, int> makePositionsIdPair(const Point2& position1, const Point2& position2);
|
std::pair<int, int> makePositionsIdPair(const Point2& position1, const Point2& position2);
|
||||||
int calcShortestPaths(const WeightGraph& graph, const VertexAttachedPositions& vertexAttachedPositions,
|
int calcShortestPaths(const Graph<int, int>& graph, const VertexAttachedPositions& vertexAttachedPositions,
|
||||||
const int entry, const int exit, const std::vector<int>& shortestDistances);
|
const int entry, const int exit, const std::vector<int>& shortestDistances);
|
||||||
int getNUniqueAttachedPositions(const VertexAttachedPositions& vertexAttachedPositions,
|
int getNUniqueAttachedPositions(const VertexAttachedPositions& vertexAttachedPositions,
|
||||||
const std::set<int>& vertices);
|
const std::set<int>& vertices);
|
||||||
|
@ -15,24 +15,29 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <aoc/common/Graph.hpp>
|
#include <aoc/common/GraphBase.hpp>
|
||||||
#include <aoc/common/GraphPathsResult.hpp>
|
|
||||||
|
|
||||||
class WeightGraph
|
class Dijkstra
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr int getInfiniteDistance();
|
static constexpr int getInfiniteDistance();
|
||||||
int addVertex(const int weight);
|
|
||||||
void addEdge(const int vertex1, const int vertex2, const int weight);
|
class PathsResult
|
||||||
int getVertexWeight(const int vertex) const;
|
{
|
||||||
int getEdgeWeight(const int edge) const;
|
public:
|
||||||
GraphPathsResult dijkstra(const int source) const;
|
PathsResult() : distances{}, predecessors{}
|
||||||
Graph::NeighborIterator begin(const int vertex) const;
|
{
|
||||||
Graph::NeighborIterator end() const;
|
}
|
||||||
private:
|
PathsResult(const size_t size, const int initialDistance, const int initialPredecessor)
|
||||||
Graph graph_{};
|
: distances(size, initialDistance), predecessors(size, initialPredecessor)
|
||||||
std::vector<int> edgeWeights_{};
|
{
|
||||||
std::vector<int> vertexWeights_{};
|
}
|
||||||
|
std::vector<int> distances;
|
||||||
|
std::vector<int> predecessors;
|
||||||
|
};
|
||||||
|
|
||||||
|
static PathsResult run(const GraphBase& graph, const std::function<int(int)>& edgeWeightFunctor, const int source);
|
||||||
};
|
};
|
@ -17,93 +17,58 @@
|
|||||||
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
#include <aoc/common/GraphBase.hpp>
|
||||||
|
|
||||||
|
template <typename TVertexData = void, typename TEdgeData = void>
|
||||||
class Graph
|
class Graph
|
||||||
|
: public GraphBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
int addVertex();
|
int addVertex()
|
||||||
void addEdge(const int vertex1, const int vertex2);
|
requires(std::is_void_v<TVertexData>)
|
||||||
size_t getNVertices() const;
|
|
||||||
size_t getNEdges() const;
|
|
||||||
bool areAdjacent(const int vertex1, const int vertex2) const;
|
|
||||||
|
|
||||||
struct Incidence
|
|
||||||
{
|
{
|
||||||
public:
|
return addVertexInternal();
|
||||||
Incidence(const int vertex, const int edge)
|
}
|
||||||
: vertex{ vertex }, edge{ edge }
|
|
||||||
{
|
|
||||||
}
|
|
||||||
int vertex;
|
|
||||||
int edge;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct NeighborIterator
|
template <typename T = TVertexData>
|
||||||
|
requires(!std::is_void_v<T>)
|
||||||
|
int addVertex(const T& data)
|
||||||
{
|
{
|
||||||
public:
|
vertexData_.push_back(data);
|
||||||
// Iterator traits.
|
return addVertexInternal();
|
||||||
using difference_type = std::ptrdiff_t;
|
}
|
||||||
using value_type = Incidence;
|
|
||||||
using pointer = const value_type*;
|
|
||||||
using reference = const value_type&;
|
|
||||||
using iterator_category = std::forward_iterator_tag;
|
|
||||||
|
|
||||||
NeighborIterator(const Graph& graph, const int vertex)
|
void addEdge(const int vertex1, const int vertex2)
|
||||||
: graph_{ graph }, incidence_{ -1 }, value_{ -1, -1 }
|
requires(std::is_void_v<TEdgeData>)
|
||||||
{
|
{
|
||||||
if (vertex >= 0 && vertex < graph_.firstVertexIncidences_.size())
|
addEdgeInternal(vertex1, vertex2);
|
||||||
{
|
}
|
||||||
setIncidence(graph_.firstVertexIncidences_[vertex]);
|
|
||||||
}
|
template <typename T = TEdgeData>
|
||||||
};
|
requires(!std::is_void_v<T>)
|
||||||
NeighborIterator& operator++()
|
void addEdge(const int vertex1, const int vertex2, const T& data)
|
||||||
{
|
{
|
||||||
if (incidence_ >= 0)
|
edgeData_.push_back(data);
|
||||||
{
|
addEdgeInternal(vertex1, vertex2);
|
||||||
setIncidence(graph_.nextIncidences_[incidence_]);
|
}
|
||||||
}
|
|
||||||
return *this;
|
template <typename T = TVertexData>
|
||||||
}
|
requires(!std::is_void_v<T>)
|
||||||
NeighborIterator operator++(int)
|
const T& getVertexData(const int vertex) const
|
||||||
{
|
{
|
||||||
NeighborIterator it = *this;
|
return vertexData_[vertex];
|
||||||
++(*this);
|
}
|
||||||
return it;
|
|
||||||
}
|
template <typename T = TEdgeData>
|
||||||
bool operator==(NeighborIterator other) const
|
requires(!std::is_void_v<T>)
|
||||||
{
|
const T& getEdgeData(int edge) const
|
||||||
return incidence_ == other.incidence_;
|
{
|
||||||
}
|
return edgeData_[edge];
|
||||||
bool operator!=(NeighborIterator other) const
|
}
|
||||||
{
|
|
||||||
return !(*this == other);
|
|
||||||
}
|
|
||||||
const reference operator*() const
|
|
||||||
{
|
|
||||||
return value_;
|
|
||||||
}
|
|
||||||
const pointer operator->() const
|
|
||||||
{
|
|
||||||
return &value_;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
const Graph& graph_;
|
|
||||||
int incidence_;
|
|
||||||
value_type value_;
|
|
||||||
void setIncidence(const int incidence)
|
|
||||||
{
|
|
||||||
incidence_ = incidence;
|
|
||||||
if (incidence_ >= 0)
|
|
||||||
{
|
|
||||||
value_ = { graph_.incidenceVertices_[incidence_], incidence_ >> 1 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
NeighborIterator begin(const int vertex) const;
|
|
||||||
NeighborIterator end() const;
|
|
||||||
private:
|
private:
|
||||||
std::vector<int> firstVertexIncidences_{};
|
std::conditional_t<std::is_void_v<TVertexData>, char, std::vector<TVertexData>> vertexData_{};
|
||||||
std::vector<int> nextIncidences_{};
|
std::conditional_t<std::is_void_v<TEdgeData>, char, std::vector<TEdgeData>> edgeData_{};
|
||||||
std::vector<int> incidenceVertices_{};
|
|
||||||
};
|
};
|
||||||
|
111
include/aoc/common/GraphBase.hpp
Normal file
111
include/aoc/common/GraphBase.hpp
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
// Solutions to the Advent Of Code 2024.
|
||||||
|
// Copyright (C) 2025 Stefan Müller
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify it under
|
||||||
|
// the terms of the GNU General Public License as published by the Free Software
|
||||||
|
// Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
// version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License along with
|
||||||
|
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class GraphBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
size_t getNVertices() const;
|
||||||
|
size_t getNEdges() const;
|
||||||
|
bool areAdjacent(const int vertex1, const int vertex2) const;
|
||||||
|
|
||||||
|
struct Incidence
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Incidence(const int vertex, const int edge)
|
||||||
|
: vertex{ vertex }, edge{ edge }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
int vertex;
|
||||||
|
int edge;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct NeighborIterator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// Iterator traits.
|
||||||
|
using difference_type = std::ptrdiff_t;
|
||||||
|
using value_type = Incidence;
|
||||||
|
using pointer = const value_type*;
|
||||||
|
using reference = const value_type&;
|
||||||
|
using iterator_category = std::forward_iterator_tag;
|
||||||
|
|
||||||
|
NeighborIterator(const GraphBase& graph, const int vertex)
|
||||||
|
: graph_{ graph }, incidence_{ -1 }, value_{ -1, -1 }
|
||||||
|
{
|
||||||
|
if (vertex >= 0 && vertex < graph_.firstVertexIncidences.size())
|
||||||
|
{
|
||||||
|
setIncidence(graph_.firstVertexIncidences[vertex]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
NeighborIterator& operator++()
|
||||||
|
{
|
||||||
|
if (incidence_ >= 0)
|
||||||
|
{
|
||||||
|
setIncidence(graph_.nextIncidences[incidence_]);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
NeighborIterator operator++(int)
|
||||||
|
{
|
||||||
|
NeighborIterator it = *this;
|
||||||
|
++(*this);
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
bool operator==(NeighborIterator other) const
|
||||||
|
{
|
||||||
|
return incidence_ == other.incidence_;
|
||||||
|
}
|
||||||
|
bool operator!=(NeighborIterator other) const
|
||||||
|
{
|
||||||
|
return !(*this == other);
|
||||||
|
}
|
||||||
|
const reference operator*() const
|
||||||
|
{
|
||||||
|
return value_;
|
||||||
|
}
|
||||||
|
const pointer operator->() const
|
||||||
|
{
|
||||||
|
return &value_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const GraphBase& graph_;
|
||||||
|
int incidence_;
|
||||||
|
value_type value_;
|
||||||
|
void setIncidence(const int incidence)
|
||||||
|
{
|
||||||
|
incidence_ = incidence;
|
||||||
|
if (incidence_ >= 0)
|
||||||
|
{
|
||||||
|
value_ = { graph_.incidenceVertices[incidence_], incidence_ >> 1 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
NeighborIterator begin(const int vertex) const;
|
||||||
|
NeighborIterator end() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::vector<int> firstVertexIncidences{};
|
||||||
|
std::vector<int> nextIncidences{};
|
||||||
|
std::vector<int> incidenceVertices{};
|
||||||
|
int addVertexInternal();
|
||||||
|
void addEdgeInternal(const int vertex1, const int vertex2);
|
||||||
|
};
|
@ -1,33 +0,0 @@
|
|||||||
// Solutions to the Advent Of Code 2024.
|
|
||||||
// Copyright (C) 2025 Stefan Müller
|
|
||||||
//
|
|
||||||
// This program is free software: you can redistribute it and/or modify it under
|
|
||||||
// the terms of the GNU General Public License as published by the Free Software
|
|
||||||
// Foundation, either version 3 of the License, or (at your option) any later
|
|
||||||
// version.
|
|
||||||
//
|
|
||||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License along with
|
|
||||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class GraphPathsResult
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
GraphPathsResult()
|
|
||||||
: distances{}, predecessors{}
|
|
||||||
{
|
|
||||||
}
|
|
||||||
GraphPathsResult(const size_t size, const int initialDistance, const int initialPredecessor)
|
|
||||||
: distances(size, initialDistance), predecessors(size, initialPredecessor)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
std::vector<int> distances;
|
|
||||||
std::vector<int> predecessors;
|
|
||||||
};
|
|
@ -1,38 +0,0 @@
|
|||||||
// Solutions to the Advent Of Code 2024.
|
|
||||||
// Copyright (C) 2025 Stefan Müller
|
|
||||||
//
|
|
||||||
// This program is free software: you can redistribute it and/or modify it under
|
|
||||||
// the terms of the GNU General Public License as published by the Free Software
|
|
||||||
// Foundation, either version 3 of the License, or (at your option) any later
|
|
||||||
// version.
|
|
||||||
//
|
|
||||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License along with
|
|
||||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <aoc/common/Graph.hpp>
|
|
||||||
|
|
||||||
class LabelGraph
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
int addVertex(const std::string& label);
|
|
||||||
void addEdge(const int vertex1, const int vertex2);
|
|
||||||
bool areAdjacent(const int vertex1, const int vertex2) const;
|
|
||||||
const std::string& getVertexLabel(const int vertex) const;
|
|
||||||
const std::map<std::string, int>& getLabelVertices() const;
|
|
||||||
Graph::NeighborIterator begin(const int vertex) const;
|
|
||||||
Graph::NeighborIterator end() const;
|
|
||||||
private:
|
|
||||||
Graph graph_{};
|
|
||||||
std::vector<std::string> vertexLabels_{};
|
|
||||||
std::map<std::string, int> labelVertices_{};
|
|
||||||
};
|
|
@ -32,9 +32,8 @@ void LanParty::processDataLine(const std::string& line)
|
|||||||
|
|
||||||
void LanParty::finish()
|
void LanParty::finish()
|
||||||
{
|
{
|
||||||
const auto& vertices = lan_.getLabelVertices();
|
auto itTx = labelMap_.lower_bound(getFirstTxComputerName());
|
||||||
auto itTx = vertices.lower_bound(getFirstTxComputerName());
|
const auto itTxEnd = labelMap_.upper_bound(getLastTxComputerName());
|
||||||
const auto itTxEnd = vertices.upper_bound(getLastTxComputerName());
|
|
||||||
while (itTx != itTxEnd)
|
while (itTx != itTxEnd)
|
||||||
{
|
{
|
||||||
computeInterconnectedThreeSetCount(itTx->second);
|
computeInterconnectedThreeSetCount(itTx->second);
|
||||||
@ -54,15 +53,16 @@ constexpr std::string LanParty::getLastTxComputerName()
|
|||||||
|
|
||||||
int LanParty::findOrAddVertex(const std::string& vertexId)
|
int LanParty::findOrAddVertex(const std::string& vertexId)
|
||||||
{
|
{
|
||||||
const auto& vertices = lan_.getLabelVertices();
|
const auto found = labelMap_.find(vertexId);
|
||||||
const auto found = vertices.find(vertexId);
|
if (found != labelMap_.end())
|
||||||
if (found != vertices.end())
|
|
||||||
{
|
{
|
||||||
return found->second;
|
return found->second;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return lan_.addVertex(vertexId);
|
int vertex = lan_.addVertex(vertexId);
|
||||||
|
labelMap_.insert({ vertexId, vertex });
|
||||||
|
return vertex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,6 +93,6 @@ void LanParty::computeInterconnectedThreeSetCount(const int vertexTx)
|
|||||||
|
|
||||||
bool LanParty::canProcessVertex(const int vertexToCheck, const int vertexTx) const
|
bool LanParty::canProcessVertex(const int vertexToCheck, const int vertexTx) const
|
||||||
{
|
{
|
||||||
const std::string& label{ lan_.getVertexLabel(vertexToCheck) };
|
const std::string& label{ lan_.getVertexData(vertexToCheck) };
|
||||||
return (label[0] != 't' || label > lan_.getVertexLabel(vertexTx));
|
return (label[0] != 't' || label > lan_.getVertexData(vertexTx));
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
RamRun::RamRun(const size_t memorySize, const int maxBytes)
|
RamRun::RamRun(const size_t memorySize, const int maxBytes)
|
||||||
: memorySize_{ memorySize }, maxBytes_{ maxBytes }, nBytes_{ 0 }, vertexReferences_{ memorySize, memorySize }
|
: memorySize_{ memorySize }, maxBytes_{ maxBytes }, nBytes_{ 0 }, vertexReferences_{ memorySize, memorySize },
|
||||||
|
edgeWeightFunctor_{ [](int x) { return 1; } }, dijkstraResult_{}
|
||||||
{
|
{
|
||||||
vertexReferences_.fill(getUnknownVertexReference());
|
vertexReferences_.fill(getUnknownVertexReference());
|
||||||
}
|
}
|
||||||
@ -47,7 +48,7 @@ void RamRun::processDataLine(const std::string& line)
|
|||||||
{
|
{
|
||||||
// Calculates initial distances.
|
// Calculates initial distances.
|
||||||
auto graph = buildGraph();
|
auto graph = buildGraph();
|
||||||
dijkstraResult_ = graph.dijkstra(vertexReferences_[0][0]);
|
dijkstraResult_ = Dijkstra::run(graph, edgeWeightFunctor_, vertexReferences_[0][0]);
|
||||||
part1 = dijkstraResult_.distances[vertexReferences_[memorySize_ - 1][memorySize_ - 1]];
|
part1 = dijkstraResult_.distances[vertexReferences_[memorySize_ - 1][memorySize_ - 1]];
|
||||||
}
|
}
|
||||||
else if (maxBytes_ < nBytes_)
|
else if (maxBytes_ < nBytes_)
|
||||||
@ -63,9 +64,9 @@ void RamRun::processDataLine(const std::string& line)
|
|||||||
// is now blocked.
|
// is now blocked.
|
||||||
resetVertexReferences();
|
resetVertexReferences();
|
||||||
auto graph = buildGraph();
|
auto graph = buildGraph();
|
||||||
dijkstraResult_ = graph.dijkstra(vertexReferences_[0][0]);
|
dijkstraResult_ = Dijkstra::run(graph, edgeWeightFunctor_, vertexReferences_[0][0]);
|
||||||
if (dijkstraResult_.distances[vertexReferences_[memorySize_ - 1][memorySize_ - 1]]
|
if (dijkstraResult_.distances[static_cast<size_t>(vertexReferences_[memorySize_ - 1][memorySize_ - 1])]
|
||||||
== WeightGraph::getInfiniteDistance())
|
== Dijkstra::getInfiniteDistance())
|
||||||
{
|
{
|
||||||
// Path is blocked.
|
// Path is blocked.
|
||||||
part2 = line;
|
part2 = line;
|
||||||
@ -105,9 +106,9 @@ bool RamRun::tryMarkCorrupted(const std::string& line, int& corruptedVertex)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WeightGraph RamRun::buildGraph()
|
Graph<void, void> RamRun::buildGraph()
|
||||||
{
|
{
|
||||||
WeightGraph graph;
|
Graph<void, void> graph;
|
||||||
for (size_t j = 0; j < vertexReferences_.getNRows(); j++)
|
for (size_t j = 0; j < vertexReferences_.getNRows(); j++)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < vertexReferences_.getNColumns(); i++)
|
for (size_t i = 0; i < vertexReferences_.getNColumns(); i++)
|
||||||
@ -119,14 +120,14 @@ WeightGraph RamRun::buildGraph()
|
|||||||
{
|
{
|
||||||
if (vertexReferences_[j][i - 1] != getNoVertexReference())
|
if (vertexReferences_[j][i - 1] != getNoVertexReference())
|
||||||
{
|
{
|
||||||
graph.addEdge(getVertex(graph, i - 1, j), v, 1);
|
graph.addEdge(getVertex(graph, i - 1, j), v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (j != 0)
|
if (j != 0)
|
||||||
{
|
{
|
||||||
if (vertexReferences_[j - 1][i] != getNoVertexReference())
|
if (vertexReferences_[j - 1][i] != getNoVertexReference())
|
||||||
{
|
{
|
||||||
graph.addEdge(getVertex(graph, i, j - 1), v, 1);
|
graph.addEdge(getVertex(graph, i, j - 1), v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -135,11 +136,11 @@ WeightGraph RamRun::buildGraph()
|
|||||||
return graph;
|
return graph;
|
||||||
}
|
}
|
||||||
|
|
||||||
int RamRun::getVertex(WeightGraph& graph, const size_t x, const size_t y)
|
int RamRun::getVertex(Graph<void, void>& graph, const size_t x, const size_t y)
|
||||||
{
|
{
|
||||||
if (vertexReferences_[y][x] == getUnknownVertexReference())
|
if (vertexReferences_[y][x] == getUnknownVertexReference())
|
||||||
{
|
{
|
||||||
vertexReferences_[y][x] = graph.addVertex(0);
|
vertexReferences_[y][x] = graph.addVertex();
|
||||||
}
|
}
|
||||||
return vertexReferences_[y][x];
|
return vertexReferences_[y][x];
|
||||||
}
|
}
|
||||||
|
@ -13,10 +13,12 @@
|
|||||||
// You should have received a copy of the GNU General Public License along with
|
// You should have received a copy of the GNU General Public License along with
|
||||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <aoc/ReindeerMaze.hpp>
|
||||||
|
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|
||||||
#include <aoc/ReindeerMaze.hpp>
|
#include <aoc/common/Dijkstra.hpp>
|
||||||
|
|
||||||
ReindeerMaze::ReindeerMaze(const int inputFileNameSuffix)
|
ReindeerMaze::ReindeerMaze(const int inputFileNameSuffix)
|
||||||
: LinesSolver{ inputFileNameSuffix }
|
: LinesSolver{ inputFileNameSuffix }
|
||||||
@ -40,12 +42,12 @@ void ReindeerMaze::finish()
|
|||||||
// do not want to hardcode this.
|
// do not want to hardcode this.
|
||||||
VertexAttachedPositions vertexAttachedPositions;
|
VertexAttachedPositions vertexAttachedPositions;
|
||||||
|
|
||||||
WeightGraph graph{};
|
Graph<int, int> graph{};
|
||||||
auto entry = graph.addVertex(0);
|
auto entry = graph.addVertex(0);
|
||||||
auto exit = graph.addVertex(0);
|
auto exit = graph.addVertex(0);
|
||||||
buildPathSegmentGraph(graph, vertexAttachedPositions, entry, exit);
|
buildPathSegmentGraph(graph, vertexAttachedPositions, entry, exit);
|
||||||
|
|
||||||
auto result = graph.dijkstra(exit);
|
auto result = Dijkstra::run(graph, [&graph](int edge) { return graph.getEdgeData(edge); }, exit);
|
||||||
part1 = result.distances[entry] / 2;
|
part1 = result.distances[entry] / 2;
|
||||||
part2 = calcShortestPaths(graph, vertexAttachedPositions, entry, exit, result.distances);
|
part2 = calcShortestPaths(graph, vertexAttachedPositions, entry, exit, result.distances);
|
||||||
}
|
}
|
||||||
@ -72,7 +74,7 @@ constexpr int ReindeerMaze::getTurnCost()
|
|||||||
|
|
||||||
// Constructs the graph of path segment incidences, starting with a graph that already contains the entry and the exit
|
// Constructs the graph of path segment incidences, starting with a graph that already contains the entry and the exit
|
||||||
// vertices.
|
// vertices.
|
||||||
void ReindeerMaze::buildPathSegmentGraph(WeightGraph& graph, VertexAttachedPositions& vertexAttachedPositions,
|
void ReindeerMaze::buildPathSegmentGraph(Graph<int, int>& graph, VertexAttachedPositions& vertexAttachedPositions,
|
||||||
const int entry, const int exit)
|
const int entry, const int exit)
|
||||||
{
|
{
|
||||||
// Uses list for work items to prevent invalidation of iterators on add.
|
// Uses list for work items to prevent invalidation of iterators on add.
|
||||||
@ -243,7 +245,7 @@ void ReindeerMaze::addCheckedIncidence(std::vector<ReindeerMazePathIncidence>& i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReindeerMaze::addPathSegmentEdges(WeightGraph& graph, const ReindeerMazePathIncidence& pathIncidence,
|
void ReindeerMaze::addPathSegmentEdges(Graph<int, int>& graph, const ReindeerMazePathIncidence& pathIncidence,
|
||||||
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences)
|
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences)
|
||||||
{
|
{
|
||||||
for (auto& otherIncidence : otherPathIncidences)
|
for (auto& otherIncidence : otherPathIncidences)
|
||||||
@ -267,13 +269,13 @@ std::pair<int, int> ReindeerMaze::makePositionsIdPair(const Point2& position1, c
|
|||||||
return std::make_pair(position1.x * offset + position1.y, position2.x * offset + position2.y);
|
return std::make_pair(position1.x * offset + position1.y, position2.x * offset + position2.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ReindeerMaze::calcShortestPaths(const WeightGraph& graph,
|
int ReindeerMaze::calcShortestPaths(const Graph<int, int>& graph,
|
||||||
const VertexAttachedPositions& vertexAttachedPositions, const int entry, const int exit,
|
const VertexAttachedPositions& vertexAttachedPositions, const int entry, const int exit,
|
||||||
const std::vector<int>& shortestDistances)
|
const std::vector<int>& shortestDistances)
|
||||||
{
|
{
|
||||||
std::set<int> shortestPathsVertices{};
|
std::set<int> shortestPathsVertices{};
|
||||||
|
|
||||||
std::stack<Graph::NeighborIterator> stack{};
|
std::stack<Graph<int, int>::NeighborIterator> stack{};
|
||||||
auto v = graph.begin(entry);
|
auto v = graph.begin(entry);
|
||||||
stack.emplace(v);
|
stack.emplace(v);
|
||||||
int cost{ 0 };
|
int cost{ 0 };
|
||||||
@ -286,7 +288,7 @@ int ReindeerMaze::calcShortestPaths(const WeightGraph& graph,
|
|||||||
auto& current = stack.top();
|
auto& current = stack.top();
|
||||||
int newCost{ 0 };
|
int newCost{ 0 };
|
||||||
while (current != graph.end() &&
|
while (current != graph.end() &&
|
||||||
((newCost = graph.getEdgeWeight(current->edge) + cost) + shortestDistances[current->vertex] >
|
((newCost = graph.getEdgeData(current->edge) + cost) + shortestDistances[current->vertex] >
|
||||||
shortestDistances[entry] ||
|
shortestDistances[entry] ||
|
||||||
stackedVertices.contains(current->vertex)))
|
stackedVertices.contains(current->vertex)))
|
||||||
{
|
{
|
||||||
@ -320,7 +322,7 @@ int ReindeerMaze::calcShortestPaths(const WeightGraph& graph,
|
|||||||
if (!stack.empty())
|
if (!stack.empty())
|
||||||
{
|
{
|
||||||
stackedVertices.erase(stack.top()->vertex);
|
stackedVertices.erase(stack.top()->vertex);
|
||||||
cost -= graph.getEdgeWeight(stack.top()->edge);
|
cost -= graph.getEdgeData(stack.top()->edge);
|
||||||
stack.top()++;
|
stack.top()++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -330,7 +332,7 @@ int ReindeerMaze::calcShortestPaths(const WeightGraph& graph,
|
|||||||
shortestPathsVertices.erase(entry);
|
shortestPathsVertices.erase(entry);
|
||||||
return std::accumulate(shortestPathsVertices.begin(), shortestPathsVertices.end(),
|
return std::accumulate(shortestPathsVertices.begin(), shortestPathsVertices.end(),
|
||||||
getNUniqueAttachedPositions(vertexAttachedPositions, shortestPathsVertices),
|
getNUniqueAttachedPositions(vertexAttachedPositions, shortestPathsVertices),
|
||||||
[&graph](int total, int x) { return total + graph.getVertexWeight(x); });
|
[&graph](int total, int x) { return total + graph.getVertexData(x); });
|
||||||
}
|
}
|
||||||
|
|
||||||
int ReindeerMaze::getNUniqueAttachedPositions(const VertexAttachedPositions& vertexAttachedPositions,
|
int ReindeerMaze::getNUniqueAttachedPositions(const VertexAttachedPositions& vertexAttachedPositions,
|
||||||
|
@ -13,41 +13,20 @@
|
|||||||
// You should have received a copy of the GNU General Public License along with
|
// You should have received a copy of the GNU General Public License along with
|
||||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#include <aoc/common/WeightGraph.hpp>
|
#include <aoc/common/Dijkstra.hpp>
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
constexpr int WeightGraph::getInfiniteDistance()
|
constexpr int Dijkstra::getInfiniteDistance()
|
||||||
{
|
{
|
||||||
return std::numeric_limits<int>::max();
|
return std::numeric_limits<int>::max();
|
||||||
}
|
}
|
||||||
|
|
||||||
int WeightGraph::addVertex(const int weight)
|
Dijkstra::PathsResult Dijkstra::run(const GraphBase& graph, const std::function<int(int)>& edgeWeightFunctor,
|
||||||
|
const int source)
|
||||||
{
|
{
|
||||||
vertexWeights_.push_back(weight);
|
PathsResult result{ graph.getNVertices(), getInfiniteDistance(), -1 };
|
||||||
return graph_.addVertex();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WeightGraph::addEdge(const int vertex1, const int vertex2, const int weight)
|
|
||||||
{
|
|
||||||
edgeWeights_.push_back(weight);
|
|
||||||
graph_.addEdge(vertex1, vertex2);
|
|
||||||
}
|
|
||||||
|
|
||||||
int WeightGraph::getVertexWeight(const int vertex) const
|
|
||||||
{
|
|
||||||
return vertexWeights_[vertex];
|
|
||||||
}
|
|
||||||
|
|
||||||
int WeightGraph::getEdgeWeight(const int edge) const
|
|
||||||
{
|
|
||||||
return edgeWeights_[edge];
|
|
||||||
}
|
|
||||||
|
|
||||||
GraphPathsResult WeightGraph::dijkstra(const int source) const
|
|
||||||
{
|
|
||||||
GraphPathsResult result(graph_.getNVertices(), getInfiniteDistance(), -1);
|
|
||||||
auto compare = [&result](int left, int right) { return result.distances[left] > result.distances[right]; };
|
auto compare = [&result](int left, int right) { return result.distances[left] > result.distances[right]; };
|
||||||
std::priority_queue<int, std::vector<int>, decltype(compare)> queue{ compare };
|
std::priority_queue<int, std::vector<int>, decltype(compare)> queue{ compare };
|
||||||
|
|
||||||
@ -59,9 +38,9 @@ GraphPathsResult WeightGraph::dijkstra(const int source) const
|
|||||||
int v{ queue.top() };
|
int v{ queue.top() };
|
||||||
queue.pop();
|
queue.pop();
|
||||||
|
|
||||||
for (auto neighbor = begin(v); neighbor != end(); ++neighbor)
|
for (auto neighbor = graph.begin(v); neighbor != graph.end(); ++neighbor)
|
||||||
{
|
{
|
||||||
int newDistance{ result.distances[v] + edgeWeights_[neighbor->edge] };
|
int newDistance{ result.distances[v] + edgeWeightFunctor(neighbor->edge) };
|
||||||
if (result.distances[neighbor->vertex] > newDistance)
|
if (result.distances[neighbor->vertex] > newDistance)
|
||||||
{
|
{
|
||||||
result.distances[neighbor->vertex] = newDistance;
|
result.distances[neighbor->vertex] = newDistance;
|
||||||
@ -73,13 +52,3 @@ GraphPathsResult WeightGraph::dijkstra(const int source) const
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Graph::NeighborIterator WeightGraph::begin(const int vertex) const
|
|
||||||
{
|
|
||||||
return graph_.begin(vertex);
|
|
||||||
}
|
|
||||||
|
|
||||||
Graph::NeighborIterator WeightGraph::end() const
|
|
||||||
{
|
|
||||||
return graph_.end();
|
|
||||||
}
|
|
@ -13,36 +13,19 @@
|
|||||||
// You should have received a copy of the GNU General Public License along with
|
// You should have received a copy of the GNU General Public License along with
|
||||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#include <aoc/common/Graph.hpp>
|
#include <aoc/common/GraphBase.hpp>
|
||||||
|
|
||||||
int Graph::addVertex()
|
size_t GraphBase::getNVertices() const
|
||||||
{
|
{
|
||||||
firstVertexIncidences_.push_back(-1);
|
return firstVertexIncidences.size();
|
||||||
return static_cast<int>(firstVertexIncidences_.size()) - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Graph::addEdge(const int vertex1, const int vertex2)
|
size_t GraphBase::getNEdges() const
|
||||||
{
|
{
|
||||||
incidenceVertices_.push_back(vertex2);
|
return nextIncidences.size() >> 1;
|
||||||
nextIncidences_.push_back(firstVertexIncidences_[vertex1]);
|
|
||||||
firstVertexIncidences_[vertex1] = static_cast<int>(incidenceVertices_.size()) - 1;
|
|
||||||
|
|
||||||
incidenceVertices_.push_back(vertex1);
|
|
||||||
nextIncidences_.push_back(firstVertexIncidences_[vertex2]);
|
|
||||||
firstVertexIncidences_[vertex2] = static_cast<int>(incidenceVertices_.size()) - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Graph::getNVertices() const
|
bool GraphBase::areAdjacent(const int vertex1, const int vertex2) const
|
||||||
{
|
|
||||||
return firstVertexIncidences_.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t Graph::getNEdges() const
|
|
||||||
{
|
|
||||||
return nextIncidences_.size() >> 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Graph::areAdjacent(const int vertex1, const int vertex2) const
|
|
||||||
{
|
{
|
||||||
auto it = begin(vertex1);
|
auto it = begin(vertex1);
|
||||||
while (it != end())
|
while (it != end())
|
||||||
@ -56,12 +39,29 @@ bool Graph::areAdjacent(const int vertex1, const int vertex2) const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Graph::NeighborIterator Graph::begin(const int vertex) const
|
GraphBase::NeighborIterator GraphBase::begin(const int vertex) const
|
||||||
{
|
{
|
||||||
return { *this, vertex };
|
return { *this, vertex };
|
||||||
}
|
}
|
||||||
|
|
||||||
Graph::NeighborIterator Graph::end() const
|
GraphBase::NeighborIterator GraphBase::end() const
|
||||||
{
|
{
|
||||||
return { *this, -1 };
|
return { *this, -1 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GraphBase::addVertexInternal()
|
||||||
|
{
|
||||||
|
firstVertexIncidences.push_back(-1);
|
||||||
|
return static_cast<int>(firstVertexIncidences.size()) - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GraphBase::addEdgeInternal(const int vertex1, const int vertex2)
|
||||||
|
{
|
||||||
|
incidenceVertices.push_back(vertex2);
|
||||||
|
nextIncidences.push_back(firstVertexIncidences[vertex1]);
|
||||||
|
firstVertexIncidences[vertex1] = static_cast<int>(incidenceVertices.size()) - 1;
|
||||||
|
|
||||||
|
incidenceVertices.push_back(vertex1);
|
||||||
|
nextIncidences.push_back(firstVertexIncidences[vertex2]);
|
||||||
|
firstVertexIncidences[vertex2] = static_cast<int>(incidenceVertices.size()) - 1;
|
||||||
|
}
|
@ -1,53 +0,0 @@
|
|||||||
// Solutions to the Advent Of Code 2024.
|
|
||||||
// Copyright (C) 2025 Stefan Müller
|
|
||||||
//
|
|
||||||
// This program is free software: you can redistribute it and/or modify it under
|
|
||||||
// the terms of the GNU General Public License as published by the Free Software
|
|
||||||
// Foundation, either version 3 of the License, or (at your option) any later
|
|
||||||
// version.
|
|
||||||
//
|
|
||||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
||||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
||||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License along with
|
|
||||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
#include <aoc/common/LabelGraph.hpp>
|
|
||||||
|
|
||||||
int LabelGraph::addVertex(const std::string& label)
|
|
||||||
{
|
|
||||||
vertexLabels_.push_back(label);
|
|
||||||
labelVertices_.insert({ label, static_cast<int>(vertexLabels_.size()) - 1 });
|
|
||||||
return graph_.addVertex();
|
|
||||||
}
|
|
||||||
|
|
||||||
void LabelGraph::addEdge(const int vertex1, const int vertex2)
|
|
||||||
{
|
|
||||||
graph_.addEdge(vertex1, vertex2);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LabelGraph::areAdjacent(const int vertex1, const int vertex2) const
|
|
||||||
{
|
|
||||||
return graph_.areAdjacent(vertex1, vertex2);
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string& LabelGraph::getVertexLabel(const int vertex) const
|
|
||||||
{
|
|
||||||
return vertexLabels_[vertex];
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::map<std::string, int>& LabelGraph::getLabelVertices() const
|
|
||||||
{
|
|
||||||
return labelVertices_;
|
|
||||||
}
|
|
||||||
|
|
||||||
Graph::NeighborIterator LabelGraph::begin(const int vertex) const
|
|
||||||
{
|
|
||||||
return graph_.begin(vertex);
|
|
||||||
}
|
|
||||||
|
|
||||||
Graph::NeighborIterator LabelGraph::end() const
|
|
||||||
{
|
|
||||||
return graph_.end();
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user