diff --git a/include/aoc/BridgeRepair.hpp b/include/aoc/BridgeRepair.hpp index fd12c26..53cb95c 100644 --- a/include/aoc/BridgeRepair.hpp +++ b/include/aoc/BridgeRepair.hpp @@ -17,10 +17,10 @@ #include -#include +#include class BridgeRepair - : public Solver + : public Solver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/CeresSearch.hpp b/include/aoc/CeresSearch.hpp index a140179..5a3f943 100644 --- a/include/aoc/CeresSearch.hpp +++ b/include/aoc/CeresSearch.hpp @@ -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 -#include +#include class CeresSearch - : public LinesSolver + : public LinesSolver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/ClawContraption.hpp b/include/aoc/ClawContraption.hpp index 84f6f68..c50e6bd 100644 --- a/include/aoc/ClawContraption.hpp +++ b/include/aoc/ClawContraption.hpp @@ -16,10 +16,10 @@ #pragma once #include -#include +#include class ClawContraption - : public Solver + : public Solver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/DiskFragmenter.hpp b/include/aoc/DiskFragmenter.hpp index 15097d3..d78410e 100644 --- a/include/aoc/DiskFragmenter.hpp +++ b/include/aoc/DiskFragmenter.hpp @@ -19,10 +19,10 @@ #include #include -#include +#include class DiskFragmenter - : public Solver + : public Solver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/GardenGroups.hpp b/include/aoc/GardenGroups.hpp index 4ff613c..728ceaf 100644 --- a/include/aoc/GardenGroups.hpp +++ b/include/aoc/GardenGroups.hpp @@ -22,7 +22,7 @@ #include class GardenGroups - : public LinesSolver + : public LinesSolver { public: GardenGroups(const int inputFileNameSuffix = 0); diff --git a/include/aoc/GuardGallivant.hpp b/include/aoc/GuardGallivant.hpp index 950b7a3..805faa3 100644 --- a/include/aoc/GuardGallivant.hpp +++ b/include/aoc/GuardGallivant.hpp @@ -23,7 +23,7 @@ #include class GuardGallivant - : public LinesSolver + : public LinesSolver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/HistorianHysteria.hpp b/include/aoc/HistorianHysteria.hpp index 8e8197a..7b4e98a 100644 --- a/include/aoc/HistorianHysteria.hpp +++ b/include/aoc/HistorianHysteria.hpp @@ -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 -#include +#include class HistorianHysteria - : public Solver + : public Solver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/HoofIt.hpp b/include/aoc/HoofIt.hpp index 5d902b5..412f0e5 100644 --- a/include/aoc/HoofIt.hpp +++ b/include/aoc/HoofIt.hpp @@ -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 class HoofIt - : public LinesSolver + : public LinesSolver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/LanParty.hpp b/include/aoc/LanParty.hpp index 79380f8..a8c3a82 100644 --- a/include/aoc/LanParty.hpp +++ b/include/aoc/LanParty.hpp @@ -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 -#include +#include -class LanParty : public Solver +class LanParty + : public Solver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/MullItOver.hpp b/include/aoc/MullItOver.hpp index ed30339..3e4f366 100644 --- a/include/aoc/MullItOver.hpp +++ b/include/aoc/MullItOver.hpp @@ -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 #include -#include +#include class MullItOver - : public Solver + : public Solver { public: MullItOver(); diff --git a/include/aoc/PlutonianPebbles.hpp b/include/aoc/PlutonianPebbles.hpp index 6cab9e3..25190bf 100644 --- a/include/aoc/PlutonianPebbles.hpp +++ b/include/aoc/PlutonianPebbles.hpp @@ -18,9 +18,10 @@ #include #include -#include +#include -class PlutonianPebbles : public Solver +class PlutonianPebbles + : public Solver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/PrintQueue.hpp b/include/aoc/PrintQueue.hpp index a8051b2..9e93d86 100644 --- a/include/aoc/PrintQueue.hpp +++ b/include/aoc/PrintQueue.hpp @@ -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 #include -#include +#include class PrintQueue - : public Solver + : public Solver { public: PrintQueue(); diff --git a/include/aoc/RedNosedReports.hpp b/include/aoc/RedNosedReports.hpp index f83f61e..51c7282 100644 --- a/include/aoc/RedNosedReports.hpp +++ b/include/aoc/RedNosedReports.hpp @@ -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 #include -#include +#include class RedNosedReports - : public Solver + : public Solver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/ReindeerMaze.hpp b/include/aoc/ReindeerMaze.hpp index 090bd59..f88c5a8 100644 --- a/include/aoc/ReindeerMaze.hpp +++ b/include/aoc/ReindeerMaze.hpp @@ -26,7 +26,7 @@ #include class ReindeerMaze - : public LinesSolver + : public LinesSolver { public: ReindeerMaze(const int inputFileNameSuffix = 0); diff --git a/include/aoc/ResonantCollinearity.hpp b/include/aoc/ResonantCollinearity.hpp index d0e8a3b..a89b5df 100644 --- a/include/aoc/ResonantCollinearity.hpp +++ b/include/aoc/ResonantCollinearity.hpp @@ -23,7 +23,7 @@ #include class ResonantCollinearity - : public LinesSolver + : public LinesSolver { public: virtual const std::string getPuzzleName() const override; diff --git a/include/aoc/RestroomRedoubt.hpp b/include/aoc/RestroomRedoubt.hpp index d1f1d4f..3684a9b 100644 --- a/include/aoc/RestroomRedoubt.hpp +++ b/include/aoc/RestroomRedoubt.hpp @@ -19,10 +19,10 @@ #include #include -#include +#include class RestroomRedoubt - : public Solver + : public Solver { public: RestroomRedoubt(const int width = 101, const int height = 103, const bool runPart2 = true); diff --git a/include/aoc/WarehouseWoes.hpp b/include/aoc/WarehouseWoes.hpp index 4a91c85..4bbea13 100644 --- a/include/aoc/WarehouseWoes.hpp +++ b/include/aoc/WarehouseWoes.hpp @@ -18,10 +18,10 @@ #include #include #include -#include +#include class WarehouseWoes - : public LinesSolver + : public LinesSolver { public: WarehouseWoes(const int inputFileNameSuffix = 0); diff --git a/src/framework/LinesSolver.cpp b/include/aoc/framework/LinesSolver-impl.hpp similarity index 62% rename from src/framework/LinesSolver.cpp rename to include/aoc/framework/LinesSolver-impl.hpp index 47e3bff..d353c87 100644 --- a/src/framework/LinesSolver.cpp +++ b/include/aoc/framework/LinesSolver-impl.hpp @@ -13,29 +13,36 @@ // You should have received a copy of the GNU General Public License along with // this program. If not, see . +#pragma once + #include -LinesSolver::LinesSolver(const int inputFileNameSuffix) - : Solver{ inputFileNameSuffix } +template +LinesSolver::LinesSolver(const int inputFileNameSuffix) + : Solver { inputFileNameSuffix } { } -void LinesSolver::processDataLine(const std::string& line) +template +void LinesSolver::processDataLine(const std::string& line) { lines.push_back(line); } -bool LinesSolver::isInBounds(const Point2& point) const +template +bool LinesSolver::isInBounds(const Point2& point) const { return lines.isInBounds(point); } -char LinesSolver::getCharAt(const Point2& point) const +template +char LinesSolver::getCharAt(const Point2& point) const { return lines.getCharAt(point); } -void LinesSolver::setCharAt(const Point2& point, const char value) +template +void LinesSolver::setCharAt(const Point2& point, const char value) { lines.setCharAt(point, value); } diff --git a/include/aoc/framework/LinesSolver-types.hpp b/include/aoc/framework/LinesSolver-types.hpp new file mode 100644 index 0000000..bfe196d --- /dev/null +++ b/include/aoc/framework/LinesSolver-types.hpp @@ -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 . + +#include + +template class LinesSolver; diff --git a/include/aoc/framework/LinesSolver.hpp b/include/aoc/framework/LinesSolver.hpp index 82711f0..d6da17b 100644 --- a/include/aoc/framework/LinesSolver.hpp +++ b/include/aoc/framework/LinesSolver.hpp @@ -21,8 +21,9 @@ #include #include +template class LinesSolver - : public Solver + : public Solver { public: LinesSolver(const int inputFileNameSuffix = 0); diff --git a/src/framework/Solver.cpp b/include/aoc/framework/Solver-impl.hpp similarity index 67% rename from src/framework/Solver.cpp rename to include/aoc/framework/Solver-impl.hpp index 77b5bca..07fd066 100644 --- a/src/framework/Solver.cpp +++ b/include/aoc/framework/Solver-impl.hpp @@ -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 . +#pragma once + #include #include -Solver::Solver(const int inputFileNameSuffix) - : inputFileNameSuffix_{ inputFileNameSuffix }, part1{ 0 }, part2{ 0 } +template +Solver::Solver(const int inputFileNameSuffix) + : inputFileNameSuffix_{ inputFileNameSuffix }, part1{}, part2{} { } -const std::string Solver::getInputFileName() const +template +const std::string Solver::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 +const T1& Solver::getResultPart1() const { return part1; } -long long int Solver::getResultPart2() const +template +const T2& Solver::getResultPart2() const { return part2; } -std::string Solver::clean(const std::string& original) const +template +std::string Solver::clean(const std::string& original) const { std::string cleaned{ original }; size_t start_pos = 0; diff --git a/include/aoc/framework/Solver-types.hpp b/include/aoc/framework/Solver-types.hpp new file mode 100644 index 0000000..1b499b4 --- /dev/null +++ b/include/aoc/framework/Solver-types.hpp @@ -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 . + +#include + +#include + +template class Solver; +template class Solver; +template class Solver; diff --git a/include/aoc/framework/Solver.hpp b/include/aoc/framework/Solver.hpp index 0d77328..69c917b 100644 --- a/include/aoc/framework/Solver.hpp +++ b/include/aoc/framework/Solver.hpp @@ -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 +template 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; diff --git a/include/aoc/framework/SolverEngine.hpp b/include/aoc/framework/SolverEngine.hpp index 43e76e6..a10172d 100644 --- a/include/aoc/framework/SolverEngine.hpp +++ b/include/aoc/framework/SolverEngine.hpp @@ -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 +#include +#include #include #include #include @@ -26,7 +28,28 @@ class SolverEngine { public: SolverEngine(const std::vector& inputPaths); - void run(Solver& solver); + + template + void 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; + } + } private: std::vector inputPaths_; std::filesystem::path tryGetValidFullInputFilePath(const std::string& inputFileName); diff --git a/src/framework/SolverEngine.cpp b/src/framework/SolverEngine.cpp index 61623a6..160c79c 100644 --- a/src/framework/SolverEngine.cpp +++ b/src/framework/SolverEngine.cpp @@ -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 -#include -#include - SolverEngine::SolverEngine(const std::vector& 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_) diff --git a/tests/include/aocTests/TestContext.hpp b/tests/include/aocTests/TestContext.hpp index d1b6710..eb1a8ff 100644 --- a/tests/include/aocTests/TestContext.hpp +++ b/tests/include/aocTests/TestContext.hpp @@ -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, const long long int expected1, const long long int expected2, + void run(const std::unique_ptr>&& solver, const long long expected1, + const long long expected2, const std::vector& inputPaths); + void run(const std::unique_ptr>&& solver, const long long expected1, + const std::string expected2, const std::vector& inputPaths); + void runPart1(const std::unique_ptr>&& solver, const long long expected, const std::vector& inputPaths); - void runPart1(const std::unique_ptr&& solver, const long long int expected, - const std::vector& inputPaths); - void runPart2(const std::unique_ptr&& solver, const long long int expected, + void runPart2(const std::unique_ptr>&& solver, const long long expected, const std::vector& inputPaths); std::vector getInputPaths() const; std::vector getExampleInputPaths() const; diff --git a/tests/src/TestCases.cpp b/tests/src/TestCases.cpp index 1f0307e..c1c62d1 100644 --- a/tests/src/TestCases.cpp +++ b/tests/src/TestCases.cpp @@ -275,10 +275,10 @@ TEST_CASE("[LanPartyTests]") TestContext test; SECTION("FullData") { - test.run(std::make_unique(), 1230, 0, test.getInputPaths()); + test.run(std::make_unique(), 1230, "", test.getInputPaths()); } SECTION("ExampleData") { - test.run(std::make_unique(), 7, 0, test.getExampleInputPaths()); + test.run(std::make_unique(), 7, "", test.getExampleInputPaths()); } } diff --git a/tests/src/TestContext.cpp b/tests/src/TestContext.cpp index 53d5393..9bbf0a1 100644 --- a/tests/src/TestContext.cpp +++ b/tests/src/TestContext.cpp @@ -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 -void TestContext::run(const std::unique_ptr&& solver, const long long int expected1, - const long long int expected2, const std::vector& inputPaths) +void TestContext::run(const std::unique_ptr>&& solver, const long long expected1, + const long long expected2, const std::vector& inputPaths) { SolverEngine solverEngine{ inputPaths }; solverEngine.run(*solver); @@ -29,7 +29,17 @@ void TestContext::run(const std::unique_ptr&& solver, const long long in REQUIRE(expected2 == solver->getResultPart2()); } -void TestContext::runPart1(const std::unique_ptr&& solver, const long long int expected, +void TestContext::run(const std::unique_ptr>&& solver, const long long expected1, + const std::string expected2, const std::vector& inputPaths) +{ + SolverEngine solverEngine{ inputPaths }; + solverEngine.run(*solver); + + REQUIRE(expected1 == solver->getResultPart1()); + REQUIRE(expected2 == solver->getResultPart2()); +} + +void TestContext::runPart1(const std::unique_ptr>&& solver, const long long expected, const std::vector& inputPaths) { SolverEngine solverEngine{ inputPaths }; @@ -38,7 +48,7 @@ void TestContext::runPart1(const std::unique_ptr&& solver, const long lo REQUIRE(expected == solver->getResultPart1()); } -void TestContext::runPart2(const std::unique_ptr&& solver, const long long int expected, +void TestContext::runPart2(const std::unique_ptr>&& solver, const long long expected, const std::vector& inputPaths) { SolverEngine solverEngine{ inputPaths };