Change Solver into template class to allow for non-integer results
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
48
include/aoc/framework/LinesSolver-impl.hpp
Normal file
48
include/aoc/framework/LinesSolver-impl.hpp
Normal 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);
|
||||
}
|
||||
18
include/aoc/framework/LinesSolver-types.hpp
Normal file
18
include/aoc/framework/LinesSolver-types.hpp
Normal 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>;
|
||||
@@ -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);
|
||||
|
||||
63
include/aoc/framework/Solver-impl.hpp
Normal file
63
include/aoc/framework/Solver-impl.hpp
Normal 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;
|
||||
}
|
||||
22
include/aoc/framework/Solver-types.hpp
Normal file
22
include/aoc/framework/Solver-types.hpp
Normal 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>;
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user