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

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

View File

@ -17,10 +17,10 @@
#include <vector>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class BridgeRepair
: public Solver
: public Solver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

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,10 +17,10 @@
#include <vector>
#include <aoc/framework/LinesSolver.hpp>
#include <aoc/framework/LinesSolver-types.hpp>
class CeresSearch
: public LinesSolver
: public LinesSolver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

View File

@ -16,10 +16,10 @@
#pragma once
#include <aoc/common/Point2.hpp>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class ClawContraption
: public Solver
: public Solver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

View File

@ -19,10 +19,10 @@
#include <vector>
#include <aoc/common/Interval.hpp>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class DiskFragmenter
: public Solver
: public Solver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

View File

@ -22,7 +22,7 @@
#include <aoc/framework/LinesSolver.hpp>
class GardenGroups
: public LinesSolver
: public LinesSolver<long long, long long>
{
public:
GardenGroups(const int inputFileNameSuffix = 0);

View File

@ -23,7 +23,7 @@
#include <aoc/framework/LinesSolver.hpp>
class GuardGallivant
: public LinesSolver
: public LinesSolver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

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,10 +17,10 @@
#include <set>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class HistorianHysteria
: public Solver
: public Solver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

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
@ -21,7 +21,7 @@
#include <aoc/framework/LinesSolver.hpp>
class HoofIt
: public LinesSolver
: public LinesSolver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

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,9 +16,10 @@
#pragma once
#include <aoc/common/Graph.hpp>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class LanParty : public Solver
class LanParty
: public Solver<long long, std::string>
{
public:
virtual const std::string getPuzzleName() const override;

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,10 +17,10 @@
#include <aoc/extra/MullData.hpp>
#include <aoc/extra/MullStates.hpp>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class MullItOver
: public Solver
: public Solver<long long, long long>
{
public:
MullItOver();

View File

@ -18,9 +18,10 @@
#include <map>
#include <vector>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class PlutonianPebbles : public Solver
class PlutonianPebbles
: public Solver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

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
@ -18,10 +18,10 @@
#include <vector>
#include <aoc/common/Grid.hpp>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class PrintQueue
: public Solver
: public Solver<long long, long long>
{
public:
PrintQueue();

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,10 +17,10 @@
#include <aoc/common/Slope.hpp>
#include <aoc/extra/RedNosedReportData.hpp>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class RedNosedReports
: public Solver
: public Solver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

View File

@ -26,7 +26,7 @@
#include <aoc/framework/LinesSolver.hpp>
class ReindeerMaze
: public LinesSolver
: public LinesSolver<long long, long long>
{
public:
ReindeerMaze(const int inputFileNameSuffix = 0);

View File

@ -23,7 +23,7 @@
#include <aoc/framework/LinesSolver.hpp>
class ResonantCollinearity
: public LinesSolver
: public LinesSolver<long long, long long>
{
public:
virtual const std::string getPuzzleName() const override;

View File

@ -19,10 +19,10 @@
#include <vector>
#include <aoc/common/Point2.hpp>
#include <aoc/framework/Solver.hpp>
#include <aoc/framework/Solver-types.hpp>
class RestroomRedoubt
: public Solver
: public Solver<long long, long long>
{
public:
RestroomRedoubt(const int width = 101, const int height = 103, const bool runPart2 = true);

View File

@ -18,10 +18,10 @@
#include <aoc/common/Lines.hpp>
#include <aoc/extra/WarehouseBoxPusher.hpp>
#include <aoc/extra/WarehouseWideBoxPusher.hpp>
#include <aoc/framework/LinesSolver.hpp>
#include <aoc/framework/LinesSolver-types.hpp>
class WarehouseWoes
: public LinesSolver
: public LinesSolver<long long, long long>
{
public:
WarehouseWoes(const int inputFileNameSuffix = 0);

View File

@ -13,29 +13,36 @@
// 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>
LinesSolver::LinesSolver(const int inputFileNameSuffix)
: Solver{ inputFileNameSuffix }
template <typename T1, typename T2>
LinesSolver<T1, T2>::LinesSolver(const int inputFileNameSuffix)
: Solver<T1, T2> { inputFileNameSuffix }
{
}
void LinesSolver::processDataLine(const std::string& line)
template <typename T1, typename T2>
void LinesSolver<T1, T2>::processDataLine(const std::string& line)
{
lines.push_back(line);
}
bool LinesSolver::isInBounds(const Point2& point) const
template <typename T1, typename T2>
bool LinesSolver<T1, T2>::isInBounds(const Point2& point) const
{
return lines.isInBounds(point);
}
char LinesSolver::getCharAt(const Point2& point) const
template <typename T1, typename T2>
char LinesSolver<T1, T2>::getCharAt(const Point2& point) const
{
return lines.getCharAt(point);
}
void LinesSolver::setCharAt(const Point2& point, const char value)
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

@ -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
@ -13,16 +13,20 @@
// 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>
Solver::Solver(const int inputFileNameSuffix)
: inputFileNameSuffix_{ inputFileNameSuffix }, part1{ 0 }, part2{ 0 }
template <typename T1, typename T2>
Solver<T1, T2>::Solver(const int inputFileNameSuffix)
: inputFileNameSuffix_{ inputFileNameSuffix }, part1{}, part2{}
{
}
const std::string Solver::getInputFileName() const
template <typename T1, typename T2>
const std::string Solver<T1, T2>::getInputFileName() const
{
std::ostringstream stream;
stream << clean(getPuzzleName());
@ -34,17 +38,20 @@ const std::string Solver::getInputFileName() const
return stream.str();
}
long long int Solver::getResultPart1() const
template <typename T1, typename T2>
const T1& Solver<T1, T2>::getResultPart1() const
{
return part1;
}
long long int Solver::getResultPart2() const
template <typename T1, typename T2>
const T2& Solver<T1, T2>::getResultPart2() const
{
return part2;
}
std::string Solver::clean(const std::string& original) const
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;

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);

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
@ -15,35 +15,11 @@
#include <aoc/framework/SolverEngine.hpp>
#include <fstream>
#include <iostream>
SolverEngine::SolverEngine(const std::vector<std::string>& inputPaths)
: inputPaths_{ inputPaths }
{
}
void SolverEngine::run(Solver& 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;
}
}
std::filesystem::path SolverEngine::tryGetValidFullInputFilePath(const std::string& inputFileName)
{
for (const auto& path : inputPaths_)

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
@ -24,11 +24,13 @@
class TestContext
{
public:
void run(const std::unique_ptr<Solver>&& solver, const long long int expected1, const long long int expected2,
void run(const std::unique_ptr<Solver<long long, long long>>&& solver, const long long expected1,
const long long expected2, const std::vector<std::string>& inputPaths);
void run(const std::unique_ptr<Solver<long long, std::string>>&& solver, const long long expected1,
const std::string expected2, const std::vector<std::string>& inputPaths);
void runPart1(const std::unique_ptr<Solver<long long, long long>>&& solver, const long long expected,
const std::vector<std::string>& inputPaths);
void runPart1(const std::unique_ptr<Solver>&& solver, const long long int expected,
const std::vector<std::string>& inputPaths);
void runPart2(const std::unique_ptr<Solver>&& solver, const long long int expected,
void runPart2(const std::unique_ptr<Solver<long long, long long>>&& solver, const long long expected,
const std::vector<std::string>& inputPaths);
std::vector<std::string> getInputPaths() const;
std::vector<std::string> getExampleInputPaths() const;

View File

@ -275,10 +275,10 @@ TEST_CASE("[LanPartyTests]")
TestContext test;
SECTION("FullData")
{
test.run(std::make_unique<LanParty>(), 1230, 0, test.getInputPaths());
test.run(std::make_unique<LanParty>(), 1230, "", test.getInputPaths());
}
SECTION("ExampleData")
{
test.run(std::make_unique<LanParty>(), 7, 0, test.getExampleInputPaths());
test.run(std::make_unique<LanParty>(), 7, "", test.getExampleInputPaths());
}
}

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
@ -19,8 +19,8 @@
#include <aoc/framework/SolverEngine.hpp>
void TestContext::run(const std::unique_ptr<Solver>&& solver, const long long int expected1,
const long long int expected2, const std::vector<std::string>& inputPaths)
void TestContext::run(const std::unique_ptr<Solver<long long, long long>>&& solver, const long long expected1,
const long long expected2, const std::vector<std::string>& inputPaths)
{
SolverEngine solverEngine{ inputPaths };
solverEngine.run(*solver);
@ -29,7 +29,17 @@ void TestContext::run(const std::unique_ptr<Solver>&& solver, const long long in
REQUIRE(expected2 == solver->getResultPart2());
}
void TestContext::runPart1(const std::unique_ptr<Solver>&& solver, const long long int expected,
void TestContext::run(const std::unique_ptr<Solver<long long, std::string>>&& solver, const long long expected1,
const std::string expected2, const std::vector<std::string>& inputPaths)
{
SolverEngine solverEngine{ inputPaths };
solverEngine.run(*solver);
REQUIRE(expected1 == solver->getResultPart1());
REQUIRE(expected2 == solver->getResultPart2());
}
void TestContext::runPart1(const std::unique_ptr<Solver<long long, long long>>&& solver, const long long expected,
const std::vector<std::string>& inputPaths)
{
SolverEngine solverEngine{ inputPaths };
@ -38,7 +48,7 @@ void TestContext::runPart1(const std::unique_ptr<Solver>&& solver, const long lo
REQUIRE(expected == solver->getResultPart1());
}
void TestContext::runPart2(const std::unique_ptr<Solver>&& solver, const long long int expected,
void TestContext::runPart2(const std::unique_ptr<Solver<long long, long long>>&& solver, const long long expected,
const std::vector<std::string>& inputPaths)
{
SolverEngine solverEngine{ inputPaths };