Change Solver into template class to allow for non-integer results

This commit is contained in:
2025-05-16 23:54:59 +02:00
parent 26dfb379a2
commit 7ad1b01879
28 changed files with 164 additions and 95 deletions

View File

@@ -0,0 +1,48 @@
// 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/>.
#pragma once
#include <aoc/framework/LinesSolver.hpp>
template <typename T1, typename T2>
LinesSolver<T1, T2>::LinesSolver(const int inputFileNameSuffix)
: Solver<T1, T2> { inputFileNameSuffix }
{
}
template <typename T1, typename T2>
void LinesSolver<T1, T2>::processDataLine(const std::string& line)
{
lines.push_back(line);
}
template <typename T1, typename T2>
bool LinesSolver<T1, T2>::isInBounds(const Point2& point) const
{
return lines.isInBounds(point);
}
template <typename T1, typename T2>
char LinesSolver<T1, T2>::getCharAt(const Point2& point) const
{
return lines.getCharAt(point);
}
template <typename T1, typename T2>
void LinesSolver<T1, T2>::setCharAt(const Point2& point, const char value)
{
lines.setCharAt(point, value);
}

View File

@@ -0,0 +1,18 @@
// 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/framework/LinesSolver-impl.hpp>
template class LinesSolver<long long, long long>;

View File

@@ -21,8 +21,9 @@
#include <aoc/common/Point2.hpp>
#include <aoc/framework/Solver.hpp>
template <typename T1, typename T2>
class LinesSolver
: public Solver
: public Solver<T1, T2>
{
public:
LinesSolver(const int inputFileNameSuffix = 0);

View File

@@ -0,0 +1,63 @@
// 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/>.
#pragma once
#include <aoc/framework/Solver.hpp>
#include <sstream>
template <typename T1, typename T2>
Solver<T1, T2>::Solver(const int inputFileNameSuffix)
: inputFileNameSuffix_{ inputFileNameSuffix }, part1{}, part2{}
{
}
template <typename T1, typename T2>
const std::string Solver<T1, T2>::getInputFileName() const
{
std::ostringstream stream;
stream << clean(getPuzzleName());
if (inputFileNameSuffix_ > 0)
{
stream << "_" << inputFileNameSuffix_;
}
stream << ".txt";
return stream.str();
}
template <typename T1, typename T2>
const T1& Solver<T1, T2>::getResultPart1() const
{
return part1;
}
template <typename T1, typename T2>
const T2& Solver<T1, T2>::getResultPart2() const
{
return part2;
}
template <typename T1, typename T2>
std::string Solver<T1, T2>::clean(const std::string& original) const
{
std::string cleaned{ original };
size_t start_pos = 0;
while ((start_pos = cleaned.find(" ", start_pos)) != std::string::npos)
{
cleaned.replace(start_pos, 1, "_");
}
return cleaned;
}

View File

@@ -0,0 +1,22 @@
// 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/framework/Solver-impl.hpp>
#include <string>
template class Solver<long long, long long>;
template class Solver<long long, std::string>;
template class Solver<std::string, std::string>;

View File

@@ -1,5 +1,5 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 Stefan Müller
// 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
@@ -17,21 +17,22 @@
#include <string>
template <typename T1, typename T2>
class Solver
{
public:
Solver(const int inputFileNameSuffix = 0);
virtual ~Solver(){};
virtual ~Solver() {};
const std::string getInputFileName() const;
virtual const std::string getPuzzleName() const = 0;
virtual const int getPuzzleDay() const = 0;
virtual void processDataLine(const std::string& line) = 0;
virtual void finish() = 0;
long long int getResultPart1() const;
long long int getResultPart2() const;
const T1& getResultPart1() const;
const T2& getResultPart2() const;
protected:
long long int part1;
long long int part2;
T1 part1;
T2 part2;
private:
int inputFileNameSuffix_;
std::string clean(const std::string& original) const;

View File

@@ -1,5 +1,5 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 Stefan Müller
// 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
@@ -16,6 +16,8 @@
#pragma once
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <memory>
#include <vector>
@@ -26,7 +28,28 @@ class SolverEngine
{
public:
SolverEngine(const std::vector<std::string>& inputPaths);
void run(Solver& solver);
template <typename T1, typename T2>
void run(Solver<T1, T2>& solver)
{
std::cout << "\n--- Day " << solver.getPuzzleDay() << ": " << solver.getPuzzleName() << " ---\n";
auto fullFilePath = tryGetValidFullInputFilePath(solver.getInputFileName());
if (fullFilePath != "")
{
std::string line;
std::ifstream inputFile{ fullFilePath };
while (std::getline(inputFile, line))
{
solver.processDataLine(line);
}
inputFile.close();
solver.finish();
std::cout << "Part 1: " << solver.getResultPart1() << "\nPart 2: " << solver.getResultPart2() << std::endl;
}
}
private:
std::vector<std::string> inputPaths_;
std::filesystem::path tryGetValidFullInputFilePath(const std::string& inputFileName);