Add Lines::findChar() to search for starting points in mazes

This commit is contained in:
2025-06-03 19:21:08 +02:00
parent 2afa527a85
commit 21e154ed34
8 changed files with 25 additions and 29 deletions

View File

@@ -25,19 +25,9 @@ const int GuardGallivant::getPuzzleDay() const
return 6;
}
void GuardGallivant::processDataLine(const std::string& line)
{
auto pos = line.find(getStartChar());
if (pos != std::string::npos)
{
start_ = Point2{ static_cast<int>(pos), static_cast<int>(lines.size()) };
}
LinesSolver::processDataLine(line);
}
void GuardGallivant::finish()
{
start_ = findChar(getStartChar());
tracePath();
}

View File

@@ -227,7 +227,7 @@ void ReindeerMaze::buildPathSegmentGraph(WeightedEdgeGraph& graph, VertexAttache
// Initializes the work list of crossing incidences.
void ReindeerMaze::initializeWorkList(std::list<ReindeerMazeCrossing>& crossings, const int entryVertex)
{
Point2 start{ findStart() };
Point2 start{ findChar(getStartChar()) };
crossings.emplace_back(start);
crossings.back().incidences.emplace_back(Point2::left, entryVertex);
addCheckedIncidence(crossings.back().incidences, start, Point2::right);
@@ -243,21 +243,6 @@ void ReindeerMaze::addCheckedIncidence(std::vector<ReindeerMazePathIncidence>& i
}
}
Point2 ReindeerMaze::findStart() const
{
for (int j = 0; j < lines.size(); j++)
{
for (int i = 0; i < lines[j].size(); i++)
{
if (lines[j][i] == getStartChar())
{
return { i, j };
}
}
}
return { 0, 0 };
}
void ReindeerMaze::addPathSegmentEdges(WeightedEdgeGraph& graph, const ReindeerMazePathIncidence& pathIncidence,
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences)
{

View File

@@ -33,6 +33,21 @@ void Lines::setCharAt(const Point2& point, const char value)
at(point.y)[point.x] = value;
}
Point2 Lines::findChar(const char c) const
{
for (int j = 0; j < size(); j++)
{
for (int i = 0; i < at(j).size(); i++)
{
if (at(j)[i] == c)
{
return { i, j };
}
}
}
return { -1, -1 };
}
std::ostream& operator<<(std::ostream& os, const Lines& lines)
{
for (const auto& line : lines)