Add solution for "Day 15: Warehouse Woes", part 1

This commit is contained in:
2025-03-04 12:06:16 +01:00
parent 8d75bdfbef
commit 02af288ebc
6 changed files with 191 additions and 0 deletions

View File

@@ -27,6 +27,24 @@ const std::array<Point2, 8> Point2::directions = { Point2::down, Point2::downRig
Point2::up, Point2::upLeft, Point2::left, Point2::downLeft };
const std::array<Point2, 4> Point2::cardinalDirections = { Point2::down, Point2::right, Point2::up, Point2::left };
Point2 Point2::getCardinalDirection(const char directionChar)
{
switch (directionChar)
{
case 'v' :
return Point2::down;
case '>' :
return Point2::right;
case '^' :
return Point2::up;
case '<' :
return Point2::left;
default :
return { 0, 0 };
}
}
Point2::Point2()
: Point2{ 0, 0 }
{

View File

@@ -35,6 +35,7 @@
#include <aoc/GardenGroups.hpp>
#include <aoc/ClawContraption.hpp>
#include <aoc/RestroomRedoubt.hpp>
#include <aoc/WarehouseWoes.hpp>
#include <aoc/LanParty.hpp>
void Program::run()
@@ -67,6 +68,7 @@ void Program::runSolvers()
runSolver<GardenGroups>(solverEngine);
runSolver<ClawContraption>(solverEngine);
runSolver<RestroomRedoubt>(solverEngine);
runSolver<WarehouseWoes>(solverEngine);
runSolver<LanParty>(solverEngine);
}

114
src/WarehouseWoes.cpp Normal file
View File

@@ -0,0 +1,114 @@
// 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/WarehouseWoes.hpp>
WarehouseWoes::WarehouseWoes(const int inputFileNameSuffix)
: LinesSolver{ inputFileNameSuffix }, isProcessingMap_{ true }, robotPosition_{}
{
}
const std::string WarehouseWoes::getPuzzleName() const
{
return "Warehouse Woes";
}
const int WarehouseWoes::getPuzzleDay() const
{
return 15;
}
void WarehouseWoes::processDataLine(const std::string& line)
{
if (!line.empty())
{
if (isProcessingMap_)
{
auto hit = line.find(getRobotChar());
if (hit != std::string::npos)
{
robotPosition_ = { static_cast<int>(hit), static_cast<int>(lines.size()) };
}
LinesSolver::processDataLine(line);
}
else
{
processDirections(line);
}
}
else
{
isProcessingMap_ = false;
}
}
void WarehouseWoes::finish()
{
for (size_t j = 0; j < lines.size(); j++)
{
for (size_t i = 0; i < lines[j].size(); i++)
{
if (lines[j][i] == getBoxChar())
{
part1 += calcGpsCoordinate(i, j);
}
}
}
}
constexpr char WarehouseWoes::getRobotChar()
{
return '@';
}
constexpr char WarehouseWoes::getBoxChar()
{
return 'O';
}
constexpr char WarehouseWoes::getWallChar()
{
return '#';
}
constexpr char WarehouseWoes::getEmptyChar()
{
return '.';
}
void WarehouseWoes::processDirections(const std::string& line)
{
for (const char c : line)
{
const Point2 direction{ Point2::getCardinalDirection(c) };
const Point2 next{ robotPosition_ + direction };
Point2 push{ next };
while (getCharAt(push) == getBoxChar())
{
push += direction;
}
if (getCharAt(push) != getWallChar())
{
setCharAt(push, getBoxChar());
setCharAt(next, getEmptyChar());
robotPosition_ = next;
}
}
}
int WarehouseWoes::calcGpsCoordinate(const size_t x, const size_t y)
{
return x + 100 * y;
}