Add solution for "Day 2: Red-Nosed Reports", part 1

This commit is contained in:
Stefan Müller 2024-12-02 14:57:18 +01:00
parent 7693c64037
commit fe67687bf7
5 changed files with 109 additions and 0 deletions

View File

@ -131,12 +131,14 @@
<ClCompile Include="AdventOfCode2024.cpp" />
<ClCompile Include="HistorianHysteria.cpp" />
<ClCompile Include="Program.cpp" />
<ClCompile Include="RedNosedReports.cpp" />
<ClCompile Include="Solver.cpp" />
<ClCompile Include="SolverEngine.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="HistorianHysteria.h" />
<ClInclude Include="Program.h" />
<ClInclude Include="RedNosedReports.h" />
<ClInclude Include="Solver.h" />
<ClInclude Include="SolverEngine.h" />
</ItemGroup>

View File

@ -30,6 +30,9 @@
<ClCompile Include="HistorianHysteria.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="RedNosedReports.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="SolverEngine.h">
@ -44,5 +47,8 @@
<ClInclude Include="HistorianHysteria.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="RedNosedReports.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -20,6 +20,7 @@
#include "SolverEngine.h"
#include "HistorianHysteria.h"
#include "RedNosedReports.h"
void Program::run()
{
@ -31,6 +32,7 @@ void Program::runSolvers()
{
SolverEngine solverEngine{ getInputPaths() };
solverEngine.run(*std::make_unique<HistorianHysteria>());
solverEngine.run(*std::make_unique<RedNosedReports>());
}
std::vector<std::string> Program::getInputPaths() const

View File

@ -0,0 +1,69 @@
// 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 <iostream>
#include <sstream>
#include "RedNosedReports.h"
#include <iomanip>
std::string RedNosedReports::getPuzzleName() const
{
return "Day 2: Red-Nosed Reports";
}
std::string RedNosedReports::getInputFileName() const
{
return "red-nosed_reports.txt";
}
void RedNosedReports::processDataLine(const std::string& line)
{
auto isSafe{ true };
auto slope{ Slope::Unknown };
std::stringstream stream{ line };
std::string token;
std::getline(stream, token, ' ');
auto prev{ std::stoi(token) };
while (isSafe && std::getline(stream, token, ' '))
{
auto next{ std::stoi(token) };
auto delta{ next - prev };
if (delta == 0 || delta > 3 || delta < -3
|| (delta > 0 && slope == Slope::Decreasing)
|| (delta < 0 && slope == Slope::Increasing))
{
isSafe = false;
}
else
{
prev = next;
if (slope == Slope::Unknown)
{
slope = delta > 0 ? Slope::Increasing : Slope::Decreasing;
}
}
}
if (isSafe)
{
part1++;
}
}
void RedNosedReports::finish()
{
}

View File

@ -0,0 +1,30 @@
// 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 RedNosedReports :
public Solver
{
public:
std::string getPuzzleName() const override;
std::string getInputFileName() const override;
void processDataLine(const std::string& line) override;
void finish() override;
};
enum class Slope { Unknown, Increasing, Decreasing };