Add solution for "Day 17: Chronospatial Computer", part 1
This commit is contained in:
85
src/ChronospatialComputer.cpp
Normal file
85
src/ChronospatialComputer.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
// 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/ChronospatialComputer.hpp>
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
ChronospatialComputer::ChronospatialComputer()
|
||||
: state_{}, program_{}, instructions_{
|
||||
std::make_unique<ChronospatialComputerDivisionInstruction>(0), // adv
|
||||
std::make_unique<ChronospatialComputerXorLiteralInstruction>(), // bxl
|
||||
std::make_unique<ChronospatialComputerModuloInstruction>(), // bst
|
||||
std::make_unique<ChronospatialComputerJumpInstruction>(), // jnz
|
||||
std::make_unique<ChronospatialComputerXorRegisterInstruction>(), // bxc
|
||||
std::make_unique<ChronospatialComputerOutInstruction>(), // out
|
||||
std::make_unique<ChronospatialComputerDivisionInstruction>(1), // bdv
|
||||
std::make_unique<ChronospatialComputerDivisionInstruction>(2) // cdv
|
||||
}
|
||||
{
|
||||
}
|
||||
|
||||
const std::string ChronospatialComputer::getPuzzleName() const
|
||||
{
|
||||
return "Chronospatial Computer";
|
||||
}
|
||||
|
||||
const int ChronospatialComputer::getPuzzleDay() const
|
||||
{
|
||||
return 17;
|
||||
}
|
||||
|
||||
void ChronospatialComputer::processDataLine(const std::string& line)
|
||||
{
|
||||
std::istringstream stream{ line };
|
||||
std::string token{};
|
||||
char c;
|
||||
int value;
|
||||
if (stream >> token)
|
||||
{
|
||||
if (token == "Register")
|
||||
{
|
||||
stream >> c >> token >> value;
|
||||
// c must be 'A', 'B', or 'C'.
|
||||
size_t regIndex{ static_cast<size_t>(c - 65) };
|
||||
state_.registers[regIndex] = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (stream >> value)
|
||||
{
|
||||
program_.push_back(value);
|
||||
// Streams a comma from between values.
|
||||
stream >> c;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ChronospatialComputer::finish()
|
||||
{
|
||||
runProgram(program_, state_);
|
||||
part1 = state_.output.str();
|
||||
}
|
||||
|
||||
void ChronospatialComputer::runProgram(const std::vector<int>& program, ChronospatialComputerState& state) const
|
||||
{
|
||||
state.instructionPointer = 0;
|
||||
while (state.instructionPointer < program.size())
|
||||
{
|
||||
instructions_[program[state.instructionPointer]]->run(state, program[state.instructionPointer + 1]);
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@
|
||||
#include <aoc/RestroomRedoubt.hpp>
|
||||
#include <aoc/WarehouseWoes.hpp>
|
||||
#include <aoc/ReindeerMaze.hpp>
|
||||
#include <aoc/ChronospatialComputer.hpp>
|
||||
#include <aoc/LanParty.hpp>
|
||||
|
||||
void Program::run()
|
||||
@@ -71,6 +72,7 @@ void Program::runSolvers()
|
||||
runSolver<RestroomRedoubt>(solverEngine);
|
||||
runSolver<WarehouseWoes>(solverEngine);
|
||||
runSolver<ReindeerMaze>(solverEngine);
|
||||
runSolver<ChronospatialComputer>(solverEngine);
|
||||
runSolver<LanParty>(solverEngine);
|
||||
}
|
||||
|
||||
|
||||
132
src/extra/ChronospatialComputerInstruction.cpp
Normal file
132
src/extra/ChronospatialComputerInstruction.cpp
Normal 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
|
||||
Reference in New Issue
Block a user