Added sub folders for common, extra, and framework code files

This commit is contained in:
2025-04-30 20:37:03 +02:00
parent 08a94ba068
commit 01c300dce1
75 changed files with 103 additions and 103 deletions

View File

@@ -0,0 +1,32 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 <map>
#include <memory>
#include <vector>
#include <aoc/common/Vertex.hpp>
class Graph
{
public:
const std::map<std::string, std::shared_ptr<Vertex>>& getVertices() const;
void addEdge(const std::string& vertexId1, const std::string& vertexId2);
private:
std::map<std::string, std::shared_ptr<Vertex>> vertices_;
std::shared_ptr<Vertex> findOrAddVertex(const std::string& vertexId);
};

View File

@@ -0,0 +1,68 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 <memory>
#include <aoc/common/Point2.hpp>
// Inspired by https://stackoverflow.com/a/32279494
template <typename T>
class Grid
{
public:
Grid(size_t nRows, size_t nColumns)
: nRows_{ nRows }, nColumns_{ nColumns }, data_{ std::make_unique<T[]>(nRows * nColumns) }
{
}
size_t getNRows() const
{
return nRows_;
}
size_t getNColumns() const
{
return nColumns_;
}
T& cell(size_t rowNo, size_t columnNo)
{
return data_[rowNo * nColumns_ + columnNo];
}
T& cell(Point2 position)
{
return data_[position.y * nColumns_ + position.x];
}
T* operator[](size_t rowNo)
{
return rowNo * nColumns_ + data_.get();
}
void fill(const T value)
{
for (size_t i = 0; i < nRows_ * nColumns_; i++)
{
data_[i] = value;
}
}
private:
size_t nRows_;
size_t nColumns_;
std::unique_ptr<T[]> data_;
};

View File

@@ -0,0 +1,27 @@
// 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/>.
#pragma once
class Interval
{
public:
Interval(const size_t start, const size_t length)
: start{ start }, length{length}
{
}
size_t start;
size_t length;
};

View File

@@ -0,0 +1,37 @@
// 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 <iostream>
#include <string>
#include <vector>
#include <aoc/common/Point2.hpp>
/// <summary>
/// A vector of strings of the same length, with methods to treat it as a rectangular, two-dimensional map of char and
/// access points on it.
/// </summary>
class Lines
: public std::vector<std::string>
{
public:
bool isInBounds(const Point2& point) const;
char getCharAt(const Point2& point) const;
void setCharAt(const Point2& point, const char value);
};
std::ostream& operator<<(std::ostream& os, const Lines& lines);

View File

@@ -0,0 +1,40 @@
// 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/>.
#pragma once
#include <tuple>
class Math
{
public:
/// <summary>
/// Calculates an integer exponentiation.
/// </summary>
/// <param name="base">Base of the exponentiation.</param>
/// <param name="exponent">Exponent of the exponentiation</param>
/// <returns>'base' raised to the power of 'exponent'.</returns>
static int ipow(const int base, const int exponent);
/// <summary>
/// Calculates the greatest common divisor gcd(a, b) and the coefficients x and y of Bézout's identity
/// ax + by = gcd(a, b). If a and b are coprime, then x is the modular multiplicative inverse of a modulo b, and y
/// is the modular multiplicative inverse of b modulo a.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns>A tuple of the gcd(a, b), x, and y.</returns>
static std::tuple<int, int, int> extendedEuclid(const int a, const int b);
};

View File

@@ -0,0 +1,52 @@
// 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 <array>
#include <iostream>
class Point2
{
public:
static const Point2 left, right, up, down;
static const Point2 upLeft, upRight, downLeft, downRight;
/// <summary>
/// The eight cardinal and diagonal directions starting down, rotating in
/// positive direction.
/// </summary>
static const std::array<Point2, 8> directions;
/// <summary>
/// The four cardinal directions starting down, rotating in positive direction.
/// </summary>
static const std::array<Point2, 4> cardinalDirections;
static Point2 getCardinalDirection(const char directionChar);
int x, y;
Point2();
Point2(const int x, const int y);
bool operator==(const Point2& rhs) const;
bool operator!=(const Point2& rhs) const;
bool operator<(const Point2& rhs) const;
Point2 operator+(const Point2& rhs) const;
Point2 operator-(const Point2& rhs) const;
Point2 operator*(const int rhs) const;
Point2 operator-() const;
Point2& operator+=(const Point2& rhs);
Point2& operator-=(const Point2& rhs);
Point2& operator*=(const int rhs);
int& operator[](size_t coordinateIndex);
};
std::ostream& operator<<(std::ostream& os, const Point2& rhs);

View File

@@ -0,0 +1,18 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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
enum class Slope { Unknown, Increasing, Decreasing };

View File

@@ -0,0 +1,31 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 <vector>
#include <aoc/common/StringStateMachine.hpp>
class StringStateMachine;
class StringState
{
public:
virtual void enter(StringStateMachine* stateMachine) = 0;
virtual void next(StringStateMachine* stateMachine) = 0;
virtual void exit(StringStateMachine* stateMachine) = 0;
virtual ~StringState() {};
};

View File

@@ -0,0 +1,36 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 <string>
#include <aoc/common/StringState.hpp>
class StringState;
class StringStateMachine
{
public:
StringStateMachine(const std::string& line, StringState& entryState);
void run();
char getCurrent() const;
void setState(StringState& state);
private:
std::string line_;
StringState* entryState_;
StringState* currentState_;
char current_;
};

View File

@@ -0,0 +1,32 @@
// Solutions to the Advent Of Code 2024.
// Copyright (C) 2024 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 <memory>
#include <string>
#include <vector>
class Vertex
{
public:
Vertex(const std::string id);
const std::string getId() const;
const std::vector<std::weak_ptr<Vertex>>& getNeighbors() const;
void addNeighbor(const std::shared_ptr<Vertex>& neighbor);
private:
const std::string id_;
std::vector<std::weak_ptr<Vertex>> neighbors_;
};

View File

@@ -0,0 +1,28 @@
// 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/>.
#pragma once
class VertexEdgeIncidence
{
public:
VertexEdgeIncidence(const int vertex, const int next)
{
this->vertex = vertex;
this->next = next;
}
int vertex;
int next;
};

View File

@@ -0,0 +1,33 @@
// 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/>.
#pragma once
#include <vector>
#include <aoc/common/VertexEdgeIncidence.hpp>
class WeightedEdgeGraph
{
public:
WeightedEdgeGraph();
int addVertex();
void addEdge(const int vertex1, const int vertex2, const int weight);
int dijkstra(const int source, const int target) const;
private:
std::vector<int> firstVertexIncidences_;
std::vector<VertexEdgeIncidence> vertexEdgeIncidences_;
std::vector<int> edgeWeights_;
};