Add solution for "Day 21: Keypad Conundrum", part 2

This commit is contained in:
2025-06-09 18:28:16 +02:00
parent ab26563e34
commit 83c66682ef
7 changed files with 156 additions and 31 deletions

View File

@@ -15,17 +15,16 @@
#include <aoc/KeypadConundrum.hpp>
#include <map>
#include <sstream>
#include <aoc/common/Point2.hpp>
#include <stack>
KeypadConundrum::KeypadConundrum()
: numericKeyboardRobot_{ { { 'A', { 0, 0 } }, { '0', { -1, 0 } }, { '1', { -2, -1 } }, { '2', { -1, -1 } },
{ '3', { 0, -1 } }, { '4', { -2, -2 } }, { '5', { -1, -2 } }, { '6', { 0, -2 } }, { '7', { -2, -3 } },
{ '8', { -1, -3 } }, { '9', { 0, -3 } } }, { -2, 0 } },
directionalKeyboardRobot_{ { { 'A', { 0, 0 } }, { '<', { -2, 1 } }, { '>', { 0, 1 } }, { '^', { -1, 0 } },
{ 'v', { -1, 1 } } }, { -2, 0 } }
{ 'v', { -1, 1 } } }, { -2, 0 } },
transformations_{}
{
}
@@ -41,15 +40,15 @@ const int KeypadConundrum::getPuzzleDay() const
void KeypadConundrum::processDataLine(const std::string& line)
{
KeypadPatternTransformation target{ numericKeyboardRobot_.calcTransformation(line) };
updateTransformationMap(target);
std::istringstream stream{ line };
int64_t number;
stream >> number;
std::string inputKeys{ numericKeyboardRobot_.calcInputKeys(line) };
for (size_t i = 0; i < 2; i++)
{
inputKeys = directionalKeyboardRobot_.calcInputKeys(inputKeys);
}
part1 += number * static_cast<int64_t>(inputKeys.size());
part1 += number * calcAccumulatedLength(target, getPart1NRobots());
part2 += number * calcAccumulatedLength(target, getPart2NRobots());
}
void KeypadConundrum::finish()
@@ -60,3 +59,69 @@ constexpr char KeypadConundrum::getStartPositionChar()
{
return 'A';
}
constexpr size_t KeypadConundrum::getPart1NRobots()
{
return 2;
}
constexpr size_t KeypadConundrum::getPart2NRobots()
{
return 25;
}
void KeypadConundrum::updateTransformationMap(KeypadPatternTransformation& targetTransformation)
{
std::stack<KeypadPatternTransformation*> stack{};
stack.push(&targetTransformation);
std::vector<KeypadPatternTransformation*> added{};
while (!stack.empty())
{
auto transformation = stack.top();
stack.pop();
for (const auto& part : transformation->parts)
{
auto it = transformations_.find(part);
if (it == transformations_.end())
{
const auto emplaceResult =
transformations_.emplace(part, directionalKeyboardRobot_.calcTransformation(part));
it = emplaceResult.first;
stack.push(&it->second);
added.push_back(&it->second);
}
transformation->partPtrs.push_back(&it->second);
}
}
updateTransformationMapLengths(added);
}
void KeypadConundrum::updateTransformationMapLengths(const std::vector<KeypadPatternTransformation*>& added)
{
for (auto& transformation : added)
{
transformation->accumulatedLengths.reserve(getPart2NRobots());
}
for (size_t i = 0; i < getPart2NRobots(); i++)
{
for (auto& transformation : added)
{
transformation->accumulatedLengths.push_back(calcAccumulatedLength(*transformation, i));
}
}
}
int64_t KeypadConundrum::calcAccumulatedLength(KeypadPatternTransformation& transformation, const size_t index)
{
int64_t n{ transformation.nDuplicates };
for (const auto& part : transformation.partPtrs)
{
n += part->accumulatedLengths[index];
}
return n;
}

View File

@@ -22,8 +22,11 @@ KeypadRobot::KeypadRobot(const std::map<char, Point2>&& keypad, const Point2&& f
{
}
std::string KeypadRobot::calcInputKeys(const std::string& targetOutputKeys) const
KeypadPatternTransformation KeypadRobot::calcTransformation(const std::string& targetOutputKeys) const
{
KeypadPatternTransformation result{};
result.accumulatedLengths.push_back(targetOutputKeys.size());
std::ostringstream stream{};
Point2 position{ 0, 0 };
for (const char c : targetOutputKeys)
@@ -32,37 +35,44 @@ std::string KeypadRobot::calcInputKeys(const std::string& targetOutputKeys) cons
// This specific order of robot arm movements aims to reduce resulting combinations of 'A' and '<' for the
// second robot, which expand to more key presses starting with the third robot.
bool horizontalFirst{ (next.x < position.x && !(next.x == forbidden_.x && position.y == forbidden_.y)) ||
(position.x == forbidden_.x && next.y == forbidden_.y) };
bool horizontalFirst{ (next.x < position.x && !isForbidden(next.x, position.y)) ||
isForbidden(position.x, next.y) };
if (horizontalFirst)
{
move(stream, next.x - position.x, '>', '<');
move(stream, next.x - position.x, '>', '<', result.nDuplicates);
}
move(stream, next.y - position.y, 'v', '^');
move(stream, next.y - position.y, 'v', '^', result.nDuplicates);
if (!horizontalFirst)
{
move(stream, next.x - position.x, '>', '<');
move(stream, next.x - position.x, '>', '<', result.nDuplicates);
}
stream << 'A';
result.parts.push_back(stream.str());
stream.str("");
position = next;
}
return stream.str();
return result;
}
void KeypadRobot::move(std::ostringstream& stream, const int delta, const char positive, const char negative) const
void KeypadRobot::move(std::ostringstream& stream, const int delta, const char positive, const char negative,
int& nDuplicates) const
{
if (delta > 0)
{
for (int i{ 0 }; i < delta; i++)
{
stream << positive;
}
stream << positive;
nDuplicates += delta - 1;
}
else
else if (delta < 0)
{
for (int i{ 0 }; i < -delta; i++)
{
stream << negative;
}
stream << negative;
nDuplicates -= delta + 1;
}
}
bool KeypadRobot::isForbidden(const int x, const int y) const
{
return x == forbidden_.x && y == forbidden_.y;
}