Add solution for "Day 14: Restroom Redoubt", part 1

This commit is contained in:
Stefan Müller 2025-02-19 14:15:34 +01:00
parent 449e33d2a3
commit a75535bab7
4 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,38 @@
// 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/>.
#pragma once
#include <array>
#include <aoc/Solver.hpp>
class RestroomRedoubt
: public Solver
{
public:
RestroomRedoubt(const int width = 101, const int height = 103);
virtual const std::string getPuzzleName() const override;
virtual const int getPuzzleDay() const override;
virtual void processDataLine(const std::string& line) override;
virtual void finish() override;
private:
const int width_;
const int height_;
const int halfWidth_;
const int halfHeight_;
std::array<int, 4> quadrants_;
static constexpr int getNPredictionSeconds();
};

View File

@ -34,6 +34,7 @@
#include <aoc/PlutonianPebbles.hpp> #include <aoc/PlutonianPebbles.hpp>
#include <aoc/GardenGroups.hpp> #include <aoc/GardenGroups.hpp>
#include <aoc/ClawContraption.hpp> #include <aoc/ClawContraption.hpp>
#include <aoc/RestroomRedoubt.hpp>
#include <aoc/LanParty.hpp> #include <aoc/LanParty.hpp>
void Program::run() void Program::run()
@ -65,6 +66,7 @@ void Program::runSolvers()
runSolver<PlutonianPebbles>(solverEngine); runSolver<PlutonianPebbles>(solverEngine);
runSolver<GardenGroups>(solverEngine); runSolver<GardenGroups>(solverEngine);
runSolver<ClawContraption>(solverEngine); runSolver<ClawContraption>(solverEngine);
runSolver<RestroomRedoubt>(solverEngine);
runSolver<LanParty>(solverEngine); runSolver<LanParty>(solverEngine);
} }

92
src/RestroomRedoubt.cpp Normal file
View File

@ -0,0 +1,92 @@
// 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/RestroomRedoubt.hpp>
#include <functional>
#include <numeric>
#include <sstream>
#include <aoc/Point2.hpp>
RestroomRedoubt::RestroomRedoubt(const int width, const int height)
: width_{ width }, height_{ height }, halfWidth_{ width / 2 }, halfHeight_{ height / 2 }
{
quadrants_.fill(0);
}
const std::string RestroomRedoubt::getPuzzleName() const
{
return "Restroom Redoubt";
}
const int RestroomRedoubt::getPuzzleDay() const
{
return 14;
}
void RestroomRedoubt::processDataLine(const std::string& line)
{
std::istringstream stream{ line };
char c;
Point2 p;
Point2 v;
stream >> c >> c >> p.x >> c >> p.y >> c >> c >> v.x >> c >> v.y;
Point2 position = p + v * getNPredictionSeconds();
position.x %= width_;
if (position.x < 0)
{
position.x += width_;
}
position.y %= height_;
if (position.y < 0)
{
position.y += height_;
}
if (position.x < halfWidth_)
{
if (position.y < halfHeight_)
{
quadrants_[0]++;
}
else if (position.y > halfHeight_)
{
quadrants_[1]++;
}
}
else if (position.x > halfWidth_)
{
if (position.y < halfHeight_)
{
quadrants_[2]++;
}
else if (position.y > halfHeight_)
{
quadrants_[3]++;
}
}
}
void RestroomRedoubt::finish()
{
part1 = std::accumulate(quadrants_.cbegin(), quadrants_.cend(), 1, std::multiplies<int>{});
}
constexpr int RestroomRedoubt::getNPredictionSeconds()
{
return 100;
}

View File

@ -31,6 +31,7 @@
#include <aoc/PlutonianPebbles.hpp> #include <aoc/PlutonianPebbles.hpp>
#include <aoc/GardenGroups.hpp> #include <aoc/GardenGroups.hpp>
#include <aoc/ClawContraption.hpp> #include <aoc/ClawContraption.hpp>
#include <aoc/RestroomRedoubt.hpp>
#include <aoc/LanParty.hpp> #include <aoc/LanParty.hpp>
#define REQUIRE_MESSAGE(cond, msg) if (!(cond)) { INFO(msg); REQUIRE(cond); } #define REQUIRE_MESSAGE(cond, msg) if (!(cond)) { INFO(msg); REQUIRE(cond); }
@ -220,6 +221,19 @@ TEST_CASE("[ClawContraptionTests]")
} }
} }
TEST_CASE("[RestroomRedoubtTests]")
{
TestContext test;
SECTION("FullData")
{
test.run(std::make_unique<RestroomRedoubt>(), 224969976, 0, test.getInputPaths());
}
SECTION("ExampleData")
{
test.run(std::make_unique<RestroomRedoubt>(11, 7), 12, 0, test.getExampleInputPaths());
}
}
TEST_CASE("[LanPartyTests]") TEST_CASE("[LanPartyTests]")
{ {
TestContext test; TestContext test;