Add solution for "Day 17: Chronospatial Computer", part 1

This commit is contained in:
2025-05-17 00:05:50 +02:00
parent 09e34b9562
commit 6d3973bd1e
10 changed files with 496 additions and 0 deletions

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 <array>
#include <memory>
#include <vector>
#include <aoc/extra/ChronospatialComputerInstruction.hpp>
#include <aoc/extra/ChronospatialComputerState.hpp>
#include <aoc/framework/Solver-types.hpp>
class ChronospatialComputer
: public Solver<std::string, long long>
{
public:
ChronospatialComputer();
virtual const std::string getPuzzleName() const override;
virtual const int getPuzzleDay() const override;
virtual void processDataLine(const std::string& line) override;
virtual void finish() override;
void runProgram(const std::vector<int>& program, ChronospatialComputerState& state) const;
private:
std::array<std::unique_ptr<ChronospatialComputerInstruction>, 8> instructions_;
std::vector<int> program_;
ChronospatialComputerState state_;
};

View File

@@ -0,0 +1,120 @@
// 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 <array>
#include <functional>
#include <aoc/extra/ChronospatialComputerOperandType.hpp>
#include <aoc/extra/ChronospatialComputerState.hpp>
class ChronospatialComputerInstruction
{
public:
ChronospatialComputerInstruction(const ChronospatialComputerOperandType type);
virtual ~ChronospatialComputerInstruction() = default;
void run(ChronospatialComputerState& state, const int operand) const;
protected:
virtual void runValue(ChronospatialComputerState& state, const int operandValue) const = 0;
private:
std::function<int(const std::array<int, 3>&, const int)> operandFunctor_;
};
#pragma region ChronospatialComputerDivisionInstruction
// Represents instruction "adv" (opcode 0), "bdv" (opcode 6), or "cdv" (opcode 7).
class ChronospatialComputerDivisionInstruction
: public ChronospatialComputerInstruction
{
public:
ChronospatialComputerDivisionInstruction(const size_t destination);
protected:
virtual void runValue(ChronospatialComputerState& state, const int operandValue) const override;
private:
size_t destination_;
};
#pragma endregion
#pragma region ChronospatialComputerXorLiteralInstruction
// Represents instruction "bxl" (opcode 1).
class ChronospatialComputerXorLiteralInstruction
: public ChronospatialComputerInstruction
{
public:
ChronospatialComputerXorLiteralInstruction();
protected:
virtual void runValue(ChronospatialComputerState& state, const int operandValue) const override;
};
#pragma endregion
#pragma region ChronospatialComputerModuloInstruction
// Represents instruction "bst" (opcode 2).
class ChronospatialComputerModuloInstruction
: public ChronospatialComputerInstruction
{
public:
ChronospatialComputerModuloInstruction();
protected:
virtual void runValue(ChronospatialComputerState& state, const int operandValue) const override;
};
#pragma endregion
#pragma region ChronospatialComputerJumpInstruction
// Represents instruction "jnz" (opcode 3).
class ChronospatialComputerJumpInstruction
: public ChronospatialComputerInstruction
{
public:
ChronospatialComputerJumpInstruction();
protected:
virtual void runValue(ChronospatialComputerState& state, const int operandValue) const override;
};
#pragma endregion
#pragma region ChronospatialComputerXorRegisterInstruction
// Represents instruction "bxc" (opcode 4).
class ChronospatialComputerXorRegisterInstruction
: public ChronospatialComputerInstruction
{
public:
ChronospatialComputerXorRegisterInstruction();
protected:
virtual void runValue(ChronospatialComputerState& state, const int operandValue) const override;
};
#pragma endregion
#pragma region ChronospatialComputerOutInstruction
// Represents instruction "out" (opcode 5).
class ChronospatialComputerOutInstruction
: public ChronospatialComputerInstruction
{
public:
ChronospatialComputerOutInstruction();
protected:
virtual void runValue(ChronospatialComputerState& state, const int operandValue) const override;
};
#pragma endregion

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/>.
#pragma once
enum class ChronospatialComputerOperandType
{
Literal,
Combo
};

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
#include <array>
#include <sstream>
class ChronospatialComputerState
{
public:
size_t instructionPointer{ 0 };
std::array<int, 3> registers{};
std::ostringstream output{};
};