Add Lines::findChar() to search for starting points in mazes
This commit is contained in:
parent
2afa527a85
commit
21e154ed34
@ -28,7 +28,6 @@ class GuardGallivant
|
|||||||
public:
|
public:
|
||||||
virtual const std::string getPuzzleName() const override;
|
virtual const std::string getPuzzleName() const override;
|
||||||
virtual const int getPuzzleDay() const override;
|
virtual const int getPuzzleDay() const override;
|
||||||
virtual void processDataLine(const std::string& line) override;
|
|
||||||
virtual void finish() override;
|
virtual void finish() override;
|
||||||
private:
|
private:
|
||||||
typedef Grid<bool> VisitGrid;
|
typedef Grid<bool> VisitGrid;
|
||||||
|
@ -44,7 +44,6 @@ class ReindeerMaze
|
|||||||
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);
|
||||||
Point2 findStart() const;
|
|
||||||
void addPathSegmentEdges(WeightedEdgeGraph& graph, const ReindeerMazePathIncidence& pathIncidence,
|
void addPathSegmentEdges(WeightedEdgeGraph& 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);
|
||||||
|
@ -32,6 +32,7 @@ class Lines
|
|||||||
bool isInBounds(const Point2& point) const;
|
bool isInBounds(const Point2& point) const;
|
||||||
char getCharAt(const Point2& point) const;
|
char getCharAt(const Point2& point) const;
|
||||||
void setCharAt(const Point2& point, const char value);
|
void setCharAt(const Point2& point, const char value);
|
||||||
|
Point2 findChar(const char c) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, const Lines& lines);
|
std::ostream& operator<<(std::ostream& os, const Lines& lines);
|
||||||
|
@ -46,3 +46,9 @@ void LinesSolver<T1, T2>::setCharAt(const Point2& point, const char value)
|
|||||||
{
|
{
|
||||||
lines.setCharAt(point, value);
|
lines.setCharAt(point, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T1, typename T2>
|
||||||
|
Point2 LinesSolver<T1, T2>::findChar(const char c) const
|
||||||
|
{
|
||||||
|
return lines.findChar(c);
|
||||||
|
}
|
||||||
|
@ -33,4 +33,5 @@ class LinesSolver
|
|||||||
bool isInBounds(const Point2& point) const;
|
bool isInBounds(const Point2& point) const;
|
||||||
char getCharAt(const Point2& point) const;
|
char getCharAt(const Point2& point) const;
|
||||||
void setCharAt(const Point2& point, const char value);
|
void setCharAt(const Point2& point, const char value);
|
||||||
|
Point2 findChar(const char c) const;
|
||||||
};
|
};
|
||||||
|
@ -25,19 +25,9 @@ const int GuardGallivant::getPuzzleDay() const
|
|||||||
return 6;
|
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()
|
void GuardGallivant::finish()
|
||||||
{
|
{
|
||||||
|
start_ = findChar(getStartChar());
|
||||||
tracePath();
|
tracePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,7 +227,7 @@ void ReindeerMaze::buildPathSegmentGraph(WeightedEdgeGraph& graph, VertexAttache
|
|||||||
// Initializes the work list of crossing incidences.
|
// Initializes the work list of crossing incidences.
|
||||||
void ReindeerMaze::initializeWorkList(std::list<ReindeerMazeCrossing>& crossings, const int entryVertex)
|
void ReindeerMaze::initializeWorkList(std::list<ReindeerMazeCrossing>& crossings, const int entryVertex)
|
||||||
{
|
{
|
||||||
Point2 start{ findStart() };
|
Point2 start{ findChar(getStartChar()) };
|
||||||
crossings.emplace_back(start);
|
crossings.emplace_back(start);
|
||||||
crossings.back().incidences.emplace_back(Point2::left, entryVertex);
|
crossings.back().incidences.emplace_back(Point2::left, entryVertex);
|
||||||
addCheckedIncidence(crossings.back().incidences, start, Point2::right);
|
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,
|
void ReindeerMaze::addPathSegmentEdges(WeightedEdgeGraph& graph, const ReindeerMazePathIncidence& pathIncidence,
|
||||||
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences)
|
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences)
|
||||||
{
|
{
|
||||||
|
@ -33,6 +33,21 @@ void Lines::setCharAt(const Point2& point, const char value)
|
|||||||
at(point.y)[point.x] = 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)
|
std::ostream& operator<<(std::ostream& os, const Lines& lines)
|
||||||
{
|
{
|
||||||
for (const auto& line : lines)
|
for (const auto& line : lines)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user