Add solution for "Day 5: Print Queue", part 1

This commit is contained in:
Stefan Müller 2024-12-05 19:31:05 +01:00
parent 4f049c06d8
commit ffe2d9d443
5 changed files with 144 additions and 0 deletions

View File

@ -142,6 +142,7 @@
<ClCompile Include="MullStates.cpp" /> <ClCompile Include="MullStates.cpp" />
<ClCompile Include="MullCharState.cpp" /> <ClCompile Include="MullCharState.cpp" />
<ClCompile Include="Point2.cpp" /> <ClCompile Include="Point2.cpp" />
<ClCompile Include="PrintQueue.cpp" />
<ClCompile Include="Program.cpp" /> <ClCompile Include="Program.cpp" />
<ClCompile Include="RedNosedReports.cpp" /> <ClCompile Include="RedNosedReports.cpp" />
<ClCompile Include="Solver.cpp" /> <ClCompile Include="Solver.cpp" />
@ -163,6 +164,7 @@
<ClInclude Include="MullStates.h" /> <ClInclude Include="MullStates.h" />
<ClInclude Include="MullCharState.h" /> <ClInclude Include="MullCharState.h" />
<ClInclude Include="Point2.h" /> <ClInclude Include="Point2.h" />
<ClInclude Include="PrintQueue.h" />
<ClInclude Include="Program.h" /> <ClInclude Include="Program.h" />
<ClInclude Include="RedNosedReportData.h" /> <ClInclude Include="RedNosedReportData.h" />
<ClInclude Include="RedNosedReports.h" /> <ClInclude Include="RedNosedReports.h" />

View File

@ -75,6 +75,9 @@
<ClCompile Include="LinesSolver.cpp"> <ClCompile Include="LinesSolver.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="PrintQueue.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="SolverEngine.h"> <ClInclude Include="SolverEngine.h">
@ -140,5 +143,8 @@
<ClInclude Include="LinesSolver.h"> <ClInclude Include="LinesSolver.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="PrintQueue.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,99 @@
// 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 <sstream>
#include <vector>
#include "PrintQueue.h"
PrintQueue::PrintQueue()
: Solver{}, isProcessingOrderingRules_{ true }, orderingRules_{}
{
for (size_t i = 0; i <= maxPageNo_; i++)
{
for (size_t j = 0; j <= maxPageNo_; j++)
{
orderingRules_[i][j] = false;
}
}
}
std::string PrintQueue::getPuzzleName() const
{
return "Day 5: Print Queue";
}
std::string PrintQueue::getInputFileName() const
{
return "print_queue.txt";
}
void PrintQueue::processDataLine(const std::string& line)
{
if (isProcessingOrderingRules_)
{
if (line.empty())
{
isProcessingOrderingRules_ = false;
}
else
{
processOrderingRule(line);
}
}
else
{
processUpdatePages(line);
}
}
void PrintQueue::finish()
{
}
void PrintQueue::processOrderingRule(const std::string& line)
{
auto pos{ line.find("|") };
auto before{ std::stoi(line.substr(0, pos)) };
auto after{ std::stoi(line.substr(pos + 1)) };
orderingRules_[before][after] = true;
}
void PrintQueue::processUpdatePages(const std::string& line)
{
std::vector<size_t> pages{};
std::stringstream stream{ line };
std::string token;
auto isCorrectOrder{ true };
while (isCorrectOrder && std::getline(stream, token, ','))
{
size_t page = std::stoi(token);
size_t i{ 0 };
while (isCorrectOrder && i < pages.size())
{
isCorrectOrder = !orderingRules_[page][pages[i]];
i++;
}
if (isCorrectOrder)
{
pages.push_back(page);
}
}
if (isCorrectOrder)
{
part1 += pages[pages.size() / 2];
}
}

View 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/>.
#pragma once
#include "Solver.h"
class PrintQueue :
public Solver
{
public:
PrintQueue();
std::string getPuzzleName() const override;
std::string getInputFileName() const override;
void processDataLine(const std::string& line) override;
void finish() override;
private:
static const size_t maxPageNo_{ 99 };
bool isProcessingOrderingRules_;
bool orderingRules_[maxPageNo_ + 1][maxPageNo_ + 1];
void processOrderingRule(const std::string& line);
void processUpdatePages(const std::string& line);
};

View File

@ -24,6 +24,7 @@
#include "RedNosedReports.h" #include "RedNosedReports.h"
#include "MullItOver.h" #include "MullItOver.h"
#include "CeresSearch.h" #include "CeresSearch.h"
#include "PrintQueue.h"
void Program::run() void Program::run()
{ {
@ -38,6 +39,7 @@ void Program::runSolvers()
solverEngine.run(*std::make_unique<RedNosedReports>()); solverEngine.run(*std::make_unique<RedNosedReports>());
solverEngine.run(*std::make_unique<MullItOver>()); solverEngine.run(*std::make_unique<MullItOver>());
solverEngine.run(*std::make_unique<CeresSearch>()); solverEngine.run(*std::make_unique<CeresSearch>());
solverEngine.run(*std::make_unique<PrintQueue>());
} }
std::vector<std::string> Program::getInputPaths() const std::vector<std::string> Program::getInputPaths() const