From fe67687bf7312cec847a8d8bfcb3f96f2e14bcb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Mon, 2 Dec 2024 14:57:18 +0100 Subject: [PATCH] Add solution for "Day 2: Red-Nosed Reports", part 1 --- AdventOfCode2024/AdventOfCode2024.vcxproj | 2 + .../AdventOfCode2024.vcxproj.filters | 6 ++ AdventOfCode2024/Program.cpp | 2 + AdventOfCode2024/RedNosedReports.cpp | 69 +++++++++++++++++++ AdventOfCode2024/RedNosedReports.h | 30 ++++++++ 5 files changed, 109 insertions(+) create mode 100644 AdventOfCode2024/RedNosedReports.cpp create mode 100644 AdventOfCode2024/RedNosedReports.h diff --git a/AdventOfCode2024/AdventOfCode2024.vcxproj b/AdventOfCode2024/AdventOfCode2024.vcxproj index 7af98e5..7b8c698 100644 --- a/AdventOfCode2024/AdventOfCode2024.vcxproj +++ b/AdventOfCode2024/AdventOfCode2024.vcxproj @@ -131,12 +131,14 @@ + + diff --git a/AdventOfCode2024/AdventOfCode2024.vcxproj.filters b/AdventOfCode2024/AdventOfCode2024.vcxproj.filters index d9e4eb4..399c5a9 100644 --- a/AdventOfCode2024/AdventOfCode2024.vcxproj.filters +++ b/AdventOfCode2024/AdventOfCode2024.vcxproj.filters @@ -30,6 +30,9 @@ Source Files + + Source Files + @@ -44,5 +47,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/AdventOfCode2024/Program.cpp b/AdventOfCode2024/Program.cpp index 70f0ece..28ec101 100644 --- a/AdventOfCode2024/Program.cpp +++ b/AdventOfCode2024/Program.cpp @@ -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()); + solverEngine.run(*std::make_unique()); } std::vector Program::getInputPaths() const diff --git a/AdventOfCode2024/RedNosedReports.cpp b/AdventOfCode2024/RedNosedReports.cpp new file mode 100644 index 0000000..7587854 --- /dev/null +++ b/AdventOfCode2024/RedNosedReports.cpp @@ -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 . + +#include +#include + +#include "RedNosedReports.h" +#include + +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() +{ +} diff --git a/AdventOfCode2024/RedNosedReports.h b/AdventOfCode2024/RedNosedReports.h new file mode 100644 index 0000000..ea8cd0c --- /dev/null +++ b/AdventOfCode2024/RedNosedReports.h @@ -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 . + +#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 };