Added sub folders for common, extra, and framework code files
This commit is contained in:
39
src/extra/MullCharState.cpp
Normal file
39
src/extra/MullCharState.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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/MullCharState.hpp>
|
||||
|
||||
MullCharState::MullCharState(const char expected)
|
||||
: expected_{ expected }, successState_{}, failState_{}
|
||||
{
|
||||
}
|
||||
|
||||
void MullCharState::next(StringStateMachine* stateMachine)
|
||||
{
|
||||
if (stateMachine->getCurrent() == expected_)
|
||||
{
|
||||
stateMachine->setState(*successState_);
|
||||
}
|
||||
else
|
||||
{
|
||||
stateMachine->setState(*failState_);
|
||||
}
|
||||
}
|
||||
|
||||
void MullCharState::setTransitions(StringState& successState, StringState& failState)
|
||||
{
|
||||
successState_ = &successState;
|
||||
failState_ = &failState;
|
||||
}
|
||||
68
src/extra/MullData.cpp
Normal file
68
src/extra/MullData.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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/MullData.hpp>
|
||||
|
||||
MullData::MullData()
|
||||
: isEnabled_{ true }, factor1_{ 0 }, factor2_{ 0 }, part1_{ 0 }, part2_{ 0 }
|
||||
{
|
||||
};
|
||||
|
||||
bool MullData::getIsEnabled()
|
||||
{
|
||||
return isEnabled_;
|
||||
}
|
||||
|
||||
void MullData::setIsEnabled(const bool value)
|
||||
{
|
||||
isEnabled_ = value;
|
||||
}
|
||||
|
||||
int MullData::getFactor(const int index)
|
||||
{
|
||||
return index == 1 ? factor1_ : factor2_;
|
||||
}
|
||||
|
||||
void MullData::setFactor(const int index, const int value)
|
||||
{
|
||||
if (index == 1)
|
||||
{
|
||||
factor1_ = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
factor2_ = value;
|
||||
}
|
||||
}
|
||||
|
||||
void MullData::updateResult()
|
||||
{
|
||||
auto product = factor1_ * factor2_;
|
||||
part1_ += product;
|
||||
if (isEnabled_)
|
||||
{
|
||||
part2_ += product;
|
||||
}
|
||||
}
|
||||
|
||||
long long int MullData::getResultPart1()
|
||||
{
|
||||
return part1_;
|
||||
}
|
||||
|
||||
long long int MullData::getResultPart2()
|
||||
{
|
||||
return part2_;
|
||||
}
|
||||
21
src/extra/MullDataState.cpp
Normal file
21
src/extra/MullDataState.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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/MullDataState.hpp>
|
||||
|
||||
void MullDataState::setData(MullData& data)
|
||||
{
|
||||
data_ = &data;
|
||||
}
|
||||
39
src/extra/MullDoOpenState.cpp
Normal file
39
src/extra/MullDoOpenState.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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/MullDoOpenState.hpp>
|
||||
|
||||
void MullDoOpenState::next(StringStateMachine* stateMachine)
|
||||
{
|
||||
if (stateMachine->getCurrent() == '(' && !data_->getIsEnabled())
|
||||
{
|
||||
stateMachine->setState(*doState_);
|
||||
}
|
||||
else if (stateMachine->getCurrent() == 'n' && data_->getIsEnabled())
|
||||
{
|
||||
stateMachine->setState(*dontState_);
|
||||
}
|
||||
else
|
||||
{
|
||||
stateMachine->setState(*failState_);
|
||||
}
|
||||
}
|
||||
|
||||
void MullDoOpenState::setTransitions(StringState& doState, StringState& dontState, StringState& failState)
|
||||
{
|
||||
doState_ = &doState;
|
||||
dontState_ = &dontState;
|
||||
failState_ = &failState;
|
||||
}
|
||||
39
src/extra/MullEntryState.cpp
Normal file
39
src/extra/MullEntryState.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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/MullEntryState.hpp>
|
||||
|
||||
void MullEntryState::enter(StringStateMachine* stateMachine)
|
||||
{
|
||||
next(stateMachine);
|
||||
}
|
||||
|
||||
void MullEntryState::next(StringStateMachine* stateMachine)
|
||||
{
|
||||
if (stateMachine->getCurrent() == 'm')
|
||||
{
|
||||
stateMachine->setState(*mulState_);
|
||||
}
|
||||
else if (stateMachine->getCurrent() == 'd')
|
||||
{
|
||||
stateMachine->setState(*doState_);
|
||||
}
|
||||
}
|
||||
|
||||
void MullEntryState::setTransitions(StringState& mulState, StringState& doState)
|
||||
{
|
||||
mulState_ = &mulState;
|
||||
doState_ = &doState;
|
||||
}
|
||||
60
src/extra/MullFactorState.cpp
Normal file
60
src/extra/MullFactorState.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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/MullFactorState.hpp>
|
||||
|
||||
MullFactorState::MullFactorState(const char expected, const int index)
|
||||
: expected_{ expected }, index_{ index }, successState_{}, failState_{}
|
||||
{
|
||||
}
|
||||
|
||||
void MullFactorState::enter(StringStateMachine* stateMachine)
|
||||
{
|
||||
data_->setFactor(index_, 0);
|
||||
}
|
||||
|
||||
void MullFactorState::next(StringStateMachine* stateMachine)
|
||||
{
|
||||
if (stateMachine->getCurrent() == expected_ && data_->getFactor(index_) > 0)
|
||||
{
|
||||
if (index_ == 2)
|
||||
{
|
||||
data_->updateResult();
|
||||
}
|
||||
stateMachine->setState(*successState_);
|
||||
}
|
||||
else
|
||||
{
|
||||
int x = stateMachine->getCurrent() - '0';
|
||||
if (0 <= x && x <= 9)
|
||||
{
|
||||
data_->setFactor(index_, data_->getFactor(index_) * 10 + x);
|
||||
if (data_->getFactor(index_) > 999)
|
||||
{
|
||||
stateMachine->setState(*failState_);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stateMachine->setState(*failState_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MullFactorState::setTransitions(StringState& successState, StringState& failState)
|
||||
{
|
||||
successState_ = &successState;
|
||||
failState_ = &failState;
|
||||
}
|
||||
38
src/extra/MullStates.cpp
Normal file
38
src/extra/MullStates.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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/MullStates.hpp>
|
||||
|
||||
MullStates::MullStates(MullData& data)
|
||||
{
|
||||
entryState.setTransitions(uState, oState);
|
||||
uState.setTransitions(lState, entryState);
|
||||
lState.setTransitions(mulOpenState, entryState);
|
||||
mulOpenState.setTransitions(factor1State, entryState);
|
||||
factor1State.setData(data);
|
||||
factor1State.setTransitions(factor2State, entryState);
|
||||
factor2State.setData(data);
|
||||
factor2State.setTransitions(entryState, entryState);
|
||||
oState.setTransitions(doOpenState, entryState);
|
||||
doOpenState.setData(data);
|
||||
doOpenState.setTransitions(doCloseState, apostropheState, entryState);
|
||||
doCloseState.setData(data);
|
||||
doCloseState.setTransitions(entryState);
|
||||
apostropheState.setTransitions(tState, entryState);
|
||||
tState.setTransitions(dontOpenState, entryState);
|
||||
dontOpenState.setTransitions(dontCloseState, entryState);
|
||||
dontCloseState.setData(data);
|
||||
dontCloseState.setTransitions(entryState);
|
||||
}
|
||||
35
src/extra/MullToggleCloseState.cpp
Normal file
35
src/extra/MullToggleCloseState.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2024 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/MullToggleCloseState.hpp>
|
||||
|
||||
MullToggleCloseState::MullToggleCloseState(const bool isEnabler)
|
||||
: isEnabler_{ isEnabler }, successState_{}
|
||||
{
|
||||
}
|
||||
|
||||
void MullToggleCloseState::next(StringStateMachine* stateMachine)
|
||||
{
|
||||
if (stateMachine->getCurrent() == ')')
|
||||
{
|
||||
data_->setIsEnabled(isEnabler_);
|
||||
}
|
||||
stateMachine->setState(*successState_);
|
||||
}
|
||||
|
||||
void MullToggleCloseState::setTransitions(StringState& successState)
|
||||
{
|
||||
successState_ = &successState;
|
||||
}
|
||||
32
src/extra/ReindeerMazeCrossing.cpp
Normal file
32
src/extra/ReindeerMazeCrossing.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// 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/ReindeerMazeCrossing.hpp>
|
||||
|
||||
ReindeerMazeCrossing::ReindeerMazeCrossing(const Point2 position)
|
||||
: position_{ position }, incidences{}
|
||||
{
|
||||
}
|
||||
|
||||
Point2 ReindeerMazeCrossing::getPosition() const
|
||||
{
|
||||
return position_;
|
||||
}
|
||||
|
||||
bool ReindeerMazeCrossing::isFinished() const
|
||||
{
|
||||
return std::find_if(incidences.begin(), incidences.end(),
|
||||
[](auto& x) { return x.getPathVertex() < 0; }) == incidences.end();
|
||||
}
|
||||
51
src/extra/ReindeerMazePathIncidence.cpp
Normal file
51
src/extra/ReindeerMazePathIncidence.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
// 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/ReindeerMazePathIncidence.hpp>
|
||||
|
||||
ReindeerMazePathIncidence::ReindeerMazePathIncidence(const Point2 direction)
|
||||
: ReindeerMazePathIncidence(direction, -1)
|
||||
{
|
||||
}
|
||||
|
||||
ReindeerMazePathIncidence::ReindeerMazePathIncidence(const Point2 direction, const int pathVertex)
|
||||
: direction_{ direction }, pathVertex_{ pathVertex }, pathCost_{ 0 }
|
||||
{
|
||||
}
|
||||
|
||||
Point2 ReindeerMazePathIncidence::getDirection() const
|
||||
{
|
||||
return direction_;
|
||||
}
|
||||
|
||||
int ReindeerMazePathIncidence::getPathVertex() const
|
||||
{
|
||||
return pathVertex_;
|
||||
}
|
||||
|
||||
void ReindeerMazePathIncidence::setPathVertex(const int pathVertex)
|
||||
{
|
||||
pathVertex_ = pathVertex;
|
||||
}
|
||||
|
||||
int ReindeerMazePathIncidence::getPathCost() const
|
||||
{
|
||||
return pathCost_;
|
||||
}
|
||||
|
||||
void ReindeerMazePathIncidence::setPathCost(const int pathCost)
|
||||
{
|
||||
pathCost_ = pathCost;
|
||||
}
|
||||
53
src/extra/WarehouseBoxPusher.cpp
Normal file
53
src/extra/WarehouseBoxPusher.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
// 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/WarehouseBoxPusher.hpp>
|
||||
|
||||
WarehouseBoxPusher::WarehouseBoxPusher(const char boxChar, const char wallChar, const char emptyChar)
|
||||
: boxChar_{ boxChar }, wallChar_{ wallChar }, emptyChar_{ emptyChar }
|
||||
{
|
||||
}
|
||||
|
||||
void WarehouseBoxPusher::processMovements(Lines& warehouseMap, const std::string& line)
|
||||
{
|
||||
for (const char c : line)
|
||||
{
|
||||
processMovement(warehouseMap, Point2::getCardinalDirection(c));
|
||||
}
|
||||
}
|
||||
|
||||
void WarehouseBoxPusher::processMovement(Lines& warehouseMap, const Point2& direction)
|
||||
{
|
||||
const Point2 next{ robotPosition_ + direction };
|
||||
Point2 push{ next };
|
||||
while (warehouseMap.getCharAt(push) == boxChar_)
|
||||
{
|
||||
push += direction;
|
||||
}
|
||||
if (warehouseMap.getCharAt(push) != wallChar_)
|
||||
{
|
||||
if (push != next)
|
||||
{
|
||||
warehouseMap.setCharAt(push, boxChar_);
|
||||
warehouseMap.setCharAt(next, emptyChar_);
|
||||
}
|
||||
robotPosition_ = next;
|
||||
}
|
||||
}
|
||||
|
||||
void WarehouseBoxPusher::setRobotPosition(const Point2& robotPosition)
|
||||
{
|
||||
robotPosition_ = robotPosition;
|
||||
}
|
||||
130
src/extra/WarehouseWideBoxPusher.cpp
Normal file
130
src/extra/WarehouseWideBoxPusher.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user