Refactor graph classes
- Extract base graph logic from WeightedEdgeGraph into new Graph class - Add LabelGraph to replace old Graph class used by LanParty solver - Rename WeightedEdgeGraph to WeightGraph and change it to use new Graph class - Update affected solvers to use new graph classes
This commit is contained in:
parent
f312893ed1
commit
674553d29b
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <aoc/common/Graph.hpp>
|
#include <aoc/common/LabelGraph.hpp>
|
||||||
#include <aoc/framework/Solver-types.hpp>
|
#include <aoc/framework/Solver-types.hpp>
|
||||||
|
|
||||||
class LanParty
|
class LanParty
|
||||||
@ -27,9 +27,10 @@ class LanParty
|
|||||||
virtual void processDataLine(const std::string& line) override;
|
virtual void processDataLine(const std::string& line) override;
|
||||||
virtual void finish() override;
|
virtual void finish() override;
|
||||||
private:
|
private:
|
||||||
Graph lan_;
|
static constexpr std::string getFirstTxComputerName();
|
||||||
void computeInterconnectedThreeSetCount(const Vertex& vertexTx);
|
static constexpr std::string getLastTxComputerName();
|
||||||
bool shallProcessVertex(const Vertex& vertexToCheck, const Vertex& vertexTx) const;
|
LabelGraph lan_;
|
||||||
const std::string getFirstTxComputerName() const;
|
int findOrAddVertex(const std::string& vertexId);
|
||||||
const std::string getLastTxComputerName() const;
|
void computeInterconnectedThreeSetCount(const int vertexTx);
|
||||||
|
bool canProcessVertex(const int vertexToCheck, const int vertexTx) const;
|
||||||
};
|
};
|
||||||
|
@ -15,8 +15,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <aoc/common/GraphPathsResult.hpp>
|
||||||
#include <aoc/common/Grid.hpp>
|
#include <aoc/common/Grid.hpp>
|
||||||
#include <aoc/common/WeightedEdgeGraph.hpp>
|
#include <aoc/common/WeightGraph.hpp>
|
||||||
#include <aoc/framework/Solver-types.hpp>
|
#include <aoc/framework/Solver-types.hpp>
|
||||||
|
|
||||||
class RamRun
|
class RamRun
|
||||||
@ -33,11 +34,11 @@ class RamRun
|
|||||||
int maxBytes_;
|
int maxBytes_;
|
||||||
int nBytes_;
|
int nBytes_;
|
||||||
Grid<int> vertexReferences_;
|
Grid<int> vertexReferences_;
|
||||||
WeightedEdgeGraph::PathsResult dijkstraResult_;
|
GraphPathsResult 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);
|
||||||
WeightedEdgeGraph buildGraph();
|
WeightGraph buildGraph();
|
||||||
int getVertex(WeightedEdgeGraph& graph, const size_t x, const size_t y);
|
int getVertex(WeightGraph& 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/WeightedEdgeGraph.hpp>
|
#include <aoc/common/WeightGraph.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(WeightedEdgeGraph& graph, VertexAttachedPositions& vertexAttachedPositions,
|
void buildPathSegmentGraph(WeightGraph& 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(WeightedEdgeGraph& graph, const ReindeerMazePathIncidence& pathIncidence,
|
void addPathSegmentEdges(WeightGraph& 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 WeightedEdgeGraph& graph, const VertexAttachedPositions& vertexAttachedPositions,
|
int calcShortestPaths(const WeightGraph& 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);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// Solutions to the Advent Of Code 2024.
|
// Solutions to the Advent Of Code 2024.
|
||||||
// Copyright (C) 2024 Stefan Müller
|
// Copyright (C) 2025 Stefan Müller
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify it under
|
// 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
|
// the terms of the GNU General Public License as published by the Free Software
|
||||||
@ -15,18 +15,95 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <map>
|
#include <iterator>
|
||||||
#include <memory>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <aoc/common/Vertex.hpp>
|
|
||||||
|
|
||||||
class Graph
|
class Graph
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
const std::map<std::string, std::shared_ptr<Vertex>>& getVertices() const;
|
int addVertex();
|
||||||
void addEdge(const std::string& vertexId1, const std::string& vertexId2);
|
void addEdge(const int vertex1, const int vertex2);
|
||||||
|
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 Graph& 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 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::map<std::string, std::shared_ptr<Vertex>> vertices_;
|
std::vector<int> firstVertexIncidences_{};
|
||||||
std::shared_ptr<Vertex> findOrAddVertex(const std::string& vertexId);
|
std::vector<int> nextIncidences_{};
|
||||||
|
std::vector<int> incidenceVertices_{};
|
||||||
};
|
};
|
||||||
|
@ -15,14 +15,19 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
class VertexEdgeIncidence
|
#include <vector>
|
||||||
|
|
||||||
|
class GraphPathsResult
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
VertexEdgeIncidence(const int vertex, const int next)
|
GraphPathsResult()
|
||||||
|
: distances{}, predecessors{}
|
||||||
{
|
{
|
||||||
this->vertex = vertex;
|
|
||||||
this->next = next;
|
|
||||||
}
|
}
|
||||||
int vertex;
|
GraphPathsResult(const size_t size, const int initialDistance, const int initialPredecessor)
|
||||||
int next;
|
: distances(size, initialDistance), predecessors(size, initialPredecessor)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
std::vector<int> distances;
|
||||||
|
std::vector<int> predecessors;
|
||||||
};
|
};
|
38
include/aoc/common/LabelGraph.hpp
Normal file
38
include/aoc/common/LabelGraph.hpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// 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_{};
|
||||||
|
};
|
38
include/aoc/common/WeightGraph.hpp
Normal file
38
include/aoc/common/WeightGraph.hpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// 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>
|
||||||
|
|
||||||
|
#include <aoc/common/Graph.hpp>
|
||||||
|
#include <aoc/common/GraphPathsResult.hpp>
|
||||||
|
|
||||||
|
class WeightGraph
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static constexpr int getInfiniteDistance();
|
||||||
|
int addVertex(const int weight);
|
||||||
|
void addEdge(const int vertex1, const int vertex2, const int weight);
|
||||||
|
int getVertexWeight(const int vertex) const;
|
||||||
|
int getEdgeWeight(const int edge) const;
|
||||||
|
GraphPathsResult dijkstra(const int source) const;
|
||||||
|
Graph::NeighborIterator begin(const int vertex) const;
|
||||||
|
Graph::NeighborIterator end() const;
|
||||||
|
private:
|
||||||
|
Graph graph_{};
|
||||||
|
std::vector<int> edgeWeights_{};
|
||||||
|
std::vector<int> vertexWeights_{};
|
||||||
|
};
|
@ -1,129 +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 <iterator>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <aoc/common/VertexEdgeIncidence.hpp>
|
|
||||||
|
|
||||||
class WeightedEdgeGraph
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
WeightedEdgeGraph();
|
|
||||||
int addVertex(const int weight = 0);
|
|
||||||
void addEdge(const int vertex1, const int vertex2, const int weight);
|
|
||||||
int getVertexWeight(const int vertex) const;
|
|
||||||
int getEdgeWeight(const int edge) const;
|
|
||||||
|
|
||||||
struct PathsResult
|
|
||||||
{
|
|
||||||
PathsResult()
|
|
||||||
: distances{}, predecessors{}
|
|
||||||
{
|
|
||||||
}
|
|
||||||
PathsResult(const size_t size, const int initialDistance, const int initialPredecessor)
|
|
||||||
: distances(size, initialDistance), predecessors(size, initialPredecessor)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
std::vector<int> distances;
|
|
||||||
std::vector<int> predecessors;
|
|
||||||
};
|
|
||||||
|
|
||||||
PathsResult dijkstra(const int source) const;
|
|
||||||
static constexpr int getInfiniteDistance();
|
|
||||||
|
|
||||||
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 WeightedEdgeGraph& 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_.vertexEdgeIncidences_[incidence_].next);
|
|
||||||
}
|
|
||||||
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 WeightedEdgeGraph& graph_;
|
|
||||||
int incidence_;
|
|
||||||
value_type value_;
|
|
||||||
void setIncidence(const int incidence)
|
|
||||||
{
|
|
||||||
incidence_ = incidence;
|
|
||||||
if (incidence_ >= 0)
|
|
||||||
{
|
|
||||||
value_ = { graph_.vertexEdgeIncidences_[incidence_].vertex, incidence_ >> 1 };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
NeighborIterator begin(const int vertex) const;
|
|
||||||
NeighborIterator end() const;
|
|
||||||
private:
|
|
||||||
std::vector<int> firstVertexIncidences_;
|
|
||||||
std::vector<VertexEdgeIncidence> vertexEdgeIncidences_;
|
|
||||||
std::vector<int> edgeWeights_;
|
|
||||||
std::vector<int> vertexWeights_;
|
|
||||||
};
|
|
@ -27,58 +27,72 @@ const int LanParty::getPuzzleDay() const
|
|||||||
|
|
||||||
void LanParty::processDataLine(const std::string& line)
|
void LanParty::processDataLine(const std::string& line)
|
||||||
{
|
{
|
||||||
lan_.addEdge(line.substr(0, 2), line.substr(3, 2));
|
lan_.addEdge(findOrAddVertex(line.substr(0, 2)), findOrAddVertex(line.substr(3, 2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void LanParty::finish()
|
void LanParty::finish()
|
||||||
{
|
{
|
||||||
auto vertices = lan_.getVertices();
|
const auto& vertices = lan_.getLabelVertices();
|
||||||
// This works as long as "ta" is present as a computername, otherwise we would have to use vertices.find_if().
|
auto itTx = vertices.lower_bound(getFirstTxComputerName());
|
||||||
auto iterTx = vertices.find(getFirstTxComputerName());
|
const auto itTxEnd = vertices.upper_bound(getLastTxComputerName());
|
||||||
while (iterTx != vertices.end() && iterTx->second->getId() <= getLastTxComputerName())
|
while (itTx != itTxEnd)
|
||||||
{
|
{
|
||||||
computeInterconnectedThreeSetCount(*iterTx->second);
|
computeInterconnectedThreeSetCount(itTx->second);
|
||||||
iterTx++;
|
itTx++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LanParty::computeInterconnectedThreeSetCount(const Vertex& vertexTx)
|
constexpr std::string LanParty::getFirstTxComputerName()
|
||||||
{
|
|
||||||
auto neighborsOfTx = vertexTx.getNeighbors();
|
|
||||||
for (size_t i = 0; i < neighborsOfTx.size(); i++)
|
|
||||||
{
|
|
||||||
auto vertexA = neighborsOfTx[i].lock();
|
|
||||||
if (shallProcessVertex(*vertexA, vertexTx))
|
|
||||||
{
|
|
||||||
for (size_t j{ i + 1 }; j < neighborsOfTx.size(); j++)
|
|
||||||
{
|
|
||||||
auto vertexB = neighborsOfTx[j].lock();
|
|
||||||
if (shallProcessVertex(*vertexB, vertexTx))
|
|
||||||
{
|
|
||||||
for (auto& neighborOfA : vertexA->getNeighbors())
|
|
||||||
{
|
|
||||||
if (vertexB == neighborOfA.lock())
|
|
||||||
{
|
|
||||||
part1++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LanParty::shallProcessVertex(const Vertex& vertexToCheck, const Vertex& vertexTx) const
|
|
||||||
{
|
|
||||||
return (vertexToCheck.getId()[0] != 't' || vertexToCheck.getId() > vertexTx.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string LanParty::getFirstTxComputerName() const
|
|
||||||
{
|
{
|
||||||
return "ta";
|
return "ta";
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string LanParty::getLastTxComputerName() const
|
constexpr std::string LanParty::getLastTxComputerName()
|
||||||
{
|
{
|
||||||
return "tz";
|
return "tz";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int LanParty::findOrAddVertex(const std::string& vertexId)
|
||||||
|
{
|
||||||
|
const auto& vertices = lan_.getLabelVertices();
|
||||||
|
const auto found = vertices.find(vertexId);
|
||||||
|
if (found != vertices.end())
|
||||||
|
{
|
||||||
|
return found->second;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return lan_.addVertex(vertexId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LanParty::computeInterconnectedThreeSetCount(const int vertexTx)
|
||||||
|
{
|
||||||
|
auto itFirstNeighbor = lan_.begin(vertexTx);
|
||||||
|
while (itFirstNeighbor != lan_.end())
|
||||||
|
{
|
||||||
|
if (canProcessVertex(itFirstNeighbor->vertex, vertexTx))
|
||||||
|
{
|
||||||
|
auto itSecondNeighbor = itFirstNeighbor;
|
||||||
|
itSecondNeighbor++;
|
||||||
|
while (itSecondNeighbor != lan_.end())
|
||||||
|
{
|
||||||
|
if (canProcessVertex(itSecondNeighbor->vertex, vertexTx))
|
||||||
|
{
|
||||||
|
if (lan_.areAdjacent(itFirstNeighbor->vertex, itSecondNeighbor->vertex))
|
||||||
|
{
|
||||||
|
part1++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
itSecondNeighbor++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
itFirstNeighbor++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LanParty::canProcessVertex(const int vertexToCheck, const int vertexTx) const
|
||||||
|
{
|
||||||
|
const std::string& label{ lan_.getVertexLabel(vertexToCheck) };
|
||||||
|
return (label[0] != 't' || label > lan_.getVertexLabel(vertexTx));
|
||||||
|
}
|
||||||
|
@ -65,7 +65,7 @@ void RamRun::processDataLine(const std::string& line)
|
|||||||
auto graph = buildGraph();
|
auto graph = buildGraph();
|
||||||
dijkstraResult_ = graph.dijkstra(vertexReferences_[0][0]);
|
dijkstraResult_ = graph.dijkstra(vertexReferences_[0][0]);
|
||||||
if (dijkstraResult_.distances[vertexReferences_[memorySize_ - 1][memorySize_ - 1]]
|
if (dijkstraResult_.distances[vertexReferences_[memorySize_ - 1][memorySize_ - 1]]
|
||||||
== WeightedEdgeGraph::getInfiniteDistance())
|
== WeightGraph::getInfiniteDistance())
|
||||||
{
|
{
|
||||||
// Path is blocked.
|
// Path is blocked.
|
||||||
part2 = line;
|
part2 = line;
|
||||||
@ -105,9 +105,9 @@ bool RamRun::tryMarkCorrupted(const std::string& line, int& corruptedVertex)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WeightedEdgeGraph RamRun::buildGraph()
|
WeightGraph RamRun::buildGraph()
|
||||||
{
|
{
|
||||||
WeightedEdgeGraph graph;
|
WeightGraph 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++)
|
||||||
@ -132,15 +132,14 @@ WeightedEdgeGraph RamRun::buildGraph()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return graph;
|
return graph;
|
||||||
}
|
}
|
||||||
|
|
||||||
int RamRun::getVertex(WeightedEdgeGraph& graph, const size_t x, const size_t y)
|
int RamRun::getVertex(WeightGraph& graph, const size_t x, const size_t y)
|
||||||
{
|
{
|
||||||
if (vertexReferences_[y][x] == getUnknownVertexReference())
|
if (vertexReferences_[y][x] == getUnknownVertexReference())
|
||||||
{
|
{
|
||||||
vertexReferences_[y][x] = graph.addVertex();
|
vertexReferences_[y][x] = graph.addVertex(0);
|
||||||
}
|
}
|
||||||
return vertexReferences_[y][x];
|
return vertexReferences_[y][x];
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ void ReindeerMaze::finish()
|
|||||||
// do not want to hardcode this.
|
// do not want to hardcode this.
|
||||||
VertexAttachedPositions vertexAttachedPositions;
|
VertexAttachedPositions vertexAttachedPositions;
|
||||||
|
|
||||||
WeightedEdgeGraph graph{};
|
WeightGraph 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);
|
||||||
@ -72,7 +72,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(WeightedEdgeGraph& graph, VertexAttachedPositions& vertexAttachedPositions,
|
void ReindeerMaze::buildPathSegmentGraph(WeightGraph& 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 +243,7 @@ void ReindeerMaze::addCheckedIncidence(std::vector<ReindeerMazePathIncidence>& i
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReindeerMaze::addPathSegmentEdges(WeightedEdgeGraph& graph, const ReindeerMazePathIncidence& pathIncidence,
|
void ReindeerMaze::addPathSegmentEdges(WeightGraph& graph, const ReindeerMazePathIncidence& pathIncidence,
|
||||||
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences)
|
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences)
|
||||||
{
|
{
|
||||||
for (auto& otherIncidence : otherPathIncidences)
|
for (auto& otherIncidence : otherPathIncidences)
|
||||||
@ -267,13 +267,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 WeightedEdgeGraph& graph,
|
int ReindeerMaze::calcShortestPaths(const WeightGraph& 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<WeightedEdgeGraph::NeighborIterator> stack{};
|
std::stack<Graph::NeighborIterator> stack{};
|
||||||
auto v = graph.begin(entry);
|
auto v = graph.begin(entry);
|
||||||
stack.emplace(v);
|
stack.emplace(v);
|
||||||
int cost{ 0 };
|
int cost{ 0 };
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// Solutions to the Advent Of Code 2024.
|
// Solutions to the Advent Of Code 2024.
|
||||||
// Copyright (C) 2024 Stefan Müller
|
// Copyright (C) 2025 Stefan Müller
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify it under
|
// 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
|
// the terms of the GNU General Public License as published by the Free Software
|
||||||
@ -15,32 +15,53 @@
|
|||||||
|
|
||||||
#include <aoc/common/Graph.hpp>
|
#include <aoc/common/Graph.hpp>
|
||||||
|
|
||||||
#include <memory>
|
int Graph::addVertex()
|
||||||
|
|
||||||
const std::map<std::string, std::shared_ptr<Vertex>>& Graph::getVertices() const
|
|
||||||
{
|
{
|
||||||
return vertices_;
|
firstVertexIncidences_.push_back(-1);
|
||||||
|
return static_cast<int>(firstVertexIncidences_.size()) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Graph::addEdge(const std::string& vertexId1, const std::string& vertexId2)
|
void Graph::addEdge(const int vertex1, const int vertex2)
|
||||||
{
|
{
|
||||||
auto v1 = findOrAddVertex(vertexId1);
|
incidenceVertices_.push_back(vertex2);
|
||||||
auto v2 = findOrAddVertex(vertexId2);
|
nextIncidences_.push_back(firstVertexIncidences_[vertex1]);
|
||||||
v1->addNeighbor(v2);
|
firstVertexIncidences_[vertex1] = static_cast<int>(incidenceVertices_.size()) - 1;
|
||||||
v2->addNeighbor(v1);
|
|
||||||
|
incidenceVertices_.push_back(vertex1);
|
||||||
|
nextIncidences_.push_back(firstVertexIncidences_[vertex2]);
|
||||||
|
firstVertexIncidences_[vertex2] = static_cast<int>(incidenceVertices_.size()) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Vertex> Graph::findOrAddVertex(const std::string& vertexId)
|
size_t Graph::getNVertices() const
|
||||||
{
|
{
|
||||||
auto iter = vertices_.find(vertexId);
|
return firstVertexIncidences_.size();
|
||||||
if (iter != vertices_.end())
|
}
|
||||||
|
|
||||||
|
size_t Graph::getNEdges() const
|
||||||
|
{
|
||||||
|
return nextIncidences_.size() >> 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Graph::areAdjacent(const int vertex1, const int vertex2) const
|
||||||
|
{
|
||||||
|
auto it = begin(vertex1);
|
||||||
|
while (it != end())
|
||||||
{
|
{
|
||||||
return (*iter).second;
|
if (it->vertex == vertex2)
|
||||||
}
|
{
|
||||||
else
|
return true;
|
||||||
{
|
}
|
||||||
auto v = std::make_shared<Vertex>(vertexId);
|
it++;
|
||||||
vertices_.insert({ vertexId, v });
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Graph::NeighborIterator Graph::begin(const int vertex) const
|
||||||
|
{
|
||||||
|
return { *this, vertex };
|
||||||
|
}
|
||||||
|
|
||||||
|
Graph::NeighborIterator Graph::end() const
|
||||||
|
{
|
||||||
|
return { *this, -1 };
|
||||||
}
|
}
|
||||||
|
53
src/common/LabelGraph.cpp
Normal file
53
src/common/LabelGraph.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
// 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();
|
||||||
|
}
|
@ -13,45 +13,41 @@
|
|||||||
// 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/WeightedEdgeGraph.hpp>
|
#include <aoc/common/WeightGraph.hpp>
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
WeightedEdgeGraph::WeightedEdgeGraph()
|
constexpr int WeightGraph::getInfiniteDistance()
|
||||||
: firstVertexIncidences_{}, vertexEdgeIncidences_{}, edgeWeights_{}
|
|
||||||
{
|
{
|
||||||
|
return std::numeric_limits<int>::max();
|
||||||
}
|
}
|
||||||
|
|
||||||
int WeightedEdgeGraph::addVertex(const int weight)
|
int WeightGraph::addVertex(const int weight)
|
||||||
{
|
{
|
||||||
firstVertexIncidences_.push_back(-1);
|
|
||||||
vertexWeights_.push_back(weight);
|
vertexWeights_.push_back(weight);
|
||||||
return (int)firstVertexIncidences_.size() - 1;
|
return graph_.addVertex();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WeightedEdgeGraph::addEdge(const int vertex1, const int vertex2, const int weight)
|
void WeightGraph::addEdge(const int vertex1, const int vertex2, const int weight)
|
||||||
{
|
{
|
||||||
vertexEdgeIncidences_.emplace_back(vertex2, firstVertexIncidences_[vertex1]);
|
|
||||||
firstVertexIncidences_[vertex1] = (int)vertexEdgeIncidences_.size() - 1;
|
|
||||||
vertexEdgeIncidences_.emplace_back(vertex1, firstVertexIncidences_[vertex2]);
|
|
||||||
firstVertexIncidences_[vertex2] = (int)vertexEdgeIncidences_.size() - 1;
|
|
||||||
edgeWeights_.push_back(weight);
|
edgeWeights_.push_back(weight);
|
||||||
|
graph_.addEdge(vertex1, vertex2);
|
||||||
}
|
}
|
||||||
|
|
||||||
int WeightedEdgeGraph::getVertexWeight(const int vertex) const
|
int WeightGraph::getVertexWeight(const int vertex) const
|
||||||
{
|
{
|
||||||
return vertexWeights_[vertex];
|
return vertexWeights_[vertex];
|
||||||
}
|
}
|
||||||
|
|
||||||
int WeightedEdgeGraph::getEdgeWeight(const int edge) const
|
int WeightGraph::getEdgeWeight(const int edge) const
|
||||||
{
|
{
|
||||||
return edgeWeights_[edge];
|
return edgeWeights_[edge];
|
||||||
}
|
}
|
||||||
|
|
||||||
WeightedEdgeGraph::PathsResult WeightedEdgeGraph::dijkstra(const int source) const
|
GraphPathsResult WeightGraph::dijkstra(const int source) const
|
||||||
{
|
{
|
||||||
PathsResult result(firstVertexIncidences_.size(), getInfiniteDistance(), -1);
|
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 };
|
||||||
|
|
||||||
@ -78,17 +74,12 @@ WeightedEdgeGraph::PathsResult WeightedEdgeGraph::dijkstra(const int source) con
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr int WeightedEdgeGraph::getInfiniteDistance()
|
Graph::NeighborIterator WeightGraph::begin(const int vertex) const
|
||||||
{
|
{
|
||||||
return std::numeric_limits<int>::max();
|
return graph_.begin(vertex);
|
||||||
}
|
}
|
||||||
|
|
||||||
WeightedEdgeGraph::NeighborIterator WeightedEdgeGraph::begin(const int vertex) const
|
Graph::NeighborIterator WeightGraph::end() const
|
||||||
{
|
{
|
||||||
return { *this, vertex };
|
return graph_.end();
|
||||||
}
|
|
||||||
|
|
||||||
WeightedEdgeGraph::NeighborIterator WeightedEdgeGraph::end() const
|
|
||||||
{
|
|
||||||
return { *this, -1 };
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user