Add Lines class to allow LinesSolver to externalize access to its vector of string

This commit is contained in:
2025-03-09 15:59:23 +01:00
parent 02af288ebc
commit 047abf9772
4 changed files with 85 additions and 5 deletions

43
src/Lines.cpp Normal file
View File

@@ -0,0 +1,43 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024-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/Lines.hpp>
#include <algorithm>
bool Lines::isInBounds(const Point2& point) const
{
return 0 <= point.y && point.y < size() &&
0 <= point.x && point.x < at(point.y).size();
}
char Lines::getCharAt(const Point2& point) const
{
return at(point.y)[point.x];
}
void Lines::setCharAt(const Point2& point, const char value)
{
at(point.y)[point.x] = value;
}
std::ostream& operator<<(std::ostream& os, const Lines& lines)
{
for (const auto& line : lines)
{
os << line << std::endl;
}
return os;
}

View File

@@ -27,16 +27,15 @@ void LinesSolver::processDataLine(const std::string& line)
bool LinesSolver::isInBounds(const Point2& point) const
{
return 0 <= point.y && point.y < lines.size() &&
0 <= point.x && point.x < lines[point.y].size();
return lines.isInBounds(point);
}
char LinesSolver::getCharAt(const Point2& point) const
{
return lines[point.y][point.x];
return lines.getCharAt(point);
}
void LinesSolver::setCharAt(const Point2& point, const char value)
{
lines[point.y][point.x] = value;
lines.setCharAt(point, value);
}