131 lines
4.5 KiB
C++
131 lines
4.5 KiB
C++
// 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/WarehouseWideBoxPusher.hpp>
|
|
|
|
#include <stack>
|
|
|
|
WarehouseWideBoxPusher::WarehouseWideBoxPusher(const char leftWideBoxChar, const char rightWideBoxChar,
|
|
const char wallChar, const char emptyChar)
|
|
: WarehouseBoxPusher{ 0, wallChar, emptyChar }, leftWideBoxChar_{ leftWideBoxChar },
|
|
rightWideBoxChar_{ rightWideBoxChar }
|
|
{
|
|
}
|
|
|
|
void WarehouseWideBoxPusher::processMovement(Lines& warehouseMap, const Point2& direction)
|
|
{
|
|
const Point2 next{ robotPosition_ + direction };
|
|
|
|
if (direction.y == 0)
|
|
{
|
|
// Moves robot horizontally.
|
|
const char facingBoxChar = direction.x > 0 ? leftWideBoxChar_ : rightWideBoxChar_;
|
|
const Point2 doubleDirection{ direction.x * 2, 0 };
|
|
Point2 push{ next };
|
|
while (warehouseMap.getCharAt(push) == facingBoxChar)
|
|
{
|
|
push += doubleDirection;
|
|
}
|
|
if (warehouseMap.getCharAt(push) != wallChar_)
|
|
{
|
|
if (push != next)
|
|
{
|
|
auto& line = warehouseMap[next.y];
|
|
line.erase(line.begin() + push.x);
|
|
line.insert(line.begin() + next.x, emptyChar_);
|
|
}
|
|
robotPosition_ = next;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Moves robot vertically.
|
|
|
|
// Checks if a wall is blocking the movement directly in front of the robot.
|
|
const char charToCheck{ warehouseMap.getCharAt(next) };
|
|
if (charToCheck == wallChar_)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool canPush{ true };
|
|
std::vector<Point2> boxesToPush{};
|
|
std::stack<Point2> boxesToCheck{};
|
|
|
|
// Checks for the first box to push and stacks its left position, if any.
|
|
if (charToCheck == leftWideBoxChar_)
|
|
{
|
|
boxesToCheck.push(next);
|
|
}
|
|
else if (charToCheck == rightWideBoxChar_)
|
|
{
|
|
boxesToCheck.emplace(next.x - 1, next.y);
|
|
}
|
|
|
|
// Checks all pushed boxes.
|
|
while (!boxesToCheck.empty())
|
|
{
|
|
Point2 leftPush{ boxesToCheck.top() };
|
|
boxesToCheck.pop();
|
|
|
|
// Checks whether any wall is blocking the push.
|
|
Point2 rightPush{ leftPush + Point2::right };
|
|
const char leftCharToCheck{ warehouseMap.getCharAt(leftPush) };
|
|
const char rightCharToCheck{ warehouseMap.getCharAt(rightPush) };
|
|
if (leftCharToCheck == wallChar_ || rightCharToCheck == wallChar_)
|
|
{
|
|
canPush = false;
|
|
break;
|
|
}
|
|
|
|
// Checks if any more boxes are being pushed by this box.
|
|
if (leftCharToCheck == leftWideBoxChar_)
|
|
{
|
|
boxesToPush.push_back(leftPush);
|
|
boxesToCheck.emplace(leftPush.x, leftPush.y + direction.y);
|
|
}
|
|
else
|
|
{
|
|
if (leftCharToCheck == rightWideBoxChar_)
|
|
{
|
|
leftPush += Point2::left;
|
|
boxesToPush.push_back(leftPush);
|
|
boxesToCheck.emplace(leftPush.x, leftPush.y + direction.y);
|
|
}
|
|
if (rightCharToCheck == leftWideBoxChar_)
|
|
{
|
|
boxesToPush.push_back(rightPush);
|
|
boxesToCheck.emplace(rightPush.x, rightPush.y + direction.y);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (canPush)
|
|
{
|
|
// Moves all pushed boxes.
|
|
auto it = boxesToPush.crbegin();
|
|
while (it != boxesToPush.crend())
|
|
{
|
|
warehouseMap.setCharAt(*it, emptyChar_);
|
|
warehouseMap.setCharAt(*it + Point2::right, emptyChar_);
|
|
warehouseMap.setCharAt(*it + direction, leftWideBoxChar_);
|
|
warehouseMap.setCharAt(*it + direction + Point2::right, rightWideBoxChar_);
|
|
it++;
|
|
}
|
|
robotPosition_ = next;
|
|
}
|
|
}
|
|
}
|