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,132 @@
// 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/extra/ChronospatialComputerInstruction.hpp>
#include <aoc/common/Math.hpp>
ChronospatialComputerInstruction::ChronospatialComputerInstruction(const ChronospatialComputerOperandType type)
{
switch (type)
{
case ChronospatialComputerOperandType::Literal :
operandFunctor_ = [](const std::array<int, 3>& registers, const int operand) { return operand; };
break;
case ChronospatialComputerOperandType::Combo :
operandFunctor_ = [](const std::array<int, 3>& registers, const int operand)
{ return operand < 4 ? operand : registers[static_cast<size_t>(operand - 4)]; };
break;
}
}
void ChronospatialComputerInstruction::run(ChronospatialComputerState& state, const int operand) const
{
runValue(state, operandFunctor_(state.registers, operand));
}
#pragma region ChronospatialComputerDivisionInstruction
ChronospatialComputerDivisionInstruction::ChronospatialComputerDivisionInstruction(const size_t destination)
: ChronospatialComputerInstruction(ChronospatialComputerOperandType::Combo), destination_{ destination }
{
}
void ChronospatialComputerDivisionInstruction::runValue(ChronospatialComputerState& state, const int operandValue) const
{
state.registers[destination_] = state.registers[0] / Math::ipow(2, operandValue);
state.instructionPointer += 2;
}
#pragma endregion
#pragma region ChronospatialComputerXorLiteralInstruction
ChronospatialComputerXorLiteralInstruction::ChronospatialComputerXorLiteralInstruction()
: ChronospatialComputerInstruction(ChronospatialComputerOperandType::Literal)
{
}
void ChronospatialComputerXorLiteralInstruction::runValue(ChronospatialComputerState& state,
const int operandValue) const
{
state.registers[1] = state.registers[1] ^ operandValue;
state.instructionPointer += 2;
}
#pragma endregion
#pragma region ChronospatialComputerModuloInstruction
ChronospatialComputerModuloInstruction::ChronospatialComputerModuloInstruction()
: ChronospatialComputerInstruction(ChronospatialComputerOperandType::Combo)
{
}
void ChronospatialComputerModuloInstruction::runValue(ChronospatialComputerState& state, const int operandValue) const
{
state.registers[1] = operandValue & 0b111;
state.instructionPointer += 2;
}
#pragma endregion
#pragma region ChronospatialComputerJumpInstruction
ChronospatialComputerJumpInstruction::ChronospatialComputerJumpInstruction()
: ChronospatialComputerInstruction(ChronospatialComputerOperandType::Literal)
{
}
void ChronospatialComputerJumpInstruction::runValue(ChronospatialComputerState& state, const int operandValue) const
{
state.instructionPointer = state.registers[0] == 0 ? state.instructionPointer + 2 : operandValue;
}
#pragma endregion
#pragma region ChronospatialComputerXorRegisterInstruction
ChronospatialComputerXorRegisterInstruction::ChronospatialComputerXorRegisterInstruction()
: ChronospatialComputerInstruction(ChronospatialComputerOperandType::Literal)
{
}
void ChronospatialComputerXorRegisterInstruction::runValue(ChronospatialComputerState& state,
const int operandValue) const
{
state.registers[1] = state.registers[1] ^ state.registers[2];
state.instructionPointer += 2;
}
#pragma endregion
#pragma region ChronospatialComputerOutInstruction
ChronospatialComputerOutInstruction::ChronospatialComputerOutInstruction()
: ChronospatialComputerInstruction(ChronospatialComputerOperandType::Combo)
{
}
void ChronospatialComputerOutInstruction::runValue(ChronospatialComputerState& state, const int operandValue) const
{
if (state.output.tellp() != std::streampos(0))
{
state.output << ',';
}
state.output << (operandValue & 0b111);
state.instructionPointer += 2;
}
#pragma endregion