From ffe2d9d443c6c008dae852c05b6ce63f20974087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Thu, 5 Dec 2024 19:31:05 +0100 Subject: [PATCH] Add solution for "Day 5: Print Queue", part 1 --- AdventOfCode2024/AdventOfCode2024.vcxproj | 2 + .../AdventOfCode2024.vcxproj.filters | 6 ++ AdventOfCode2024/PrintQueue.cpp | 99 +++++++++++++++++++ AdventOfCode2024/PrintQueue.h | 35 +++++++ AdventOfCode2024/Program.cpp | 2 + 5 files changed, 144 insertions(+) create mode 100644 AdventOfCode2024/PrintQueue.cpp create mode 100644 AdventOfCode2024/PrintQueue.h diff --git a/AdventOfCode2024/AdventOfCode2024.vcxproj b/AdventOfCode2024/AdventOfCode2024.vcxproj index 46d2eb9..8b36b05 100644 --- a/AdventOfCode2024/AdventOfCode2024.vcxproj +++ b/AdventOfCode2024/AdventOfCode2024.vcxproj @@ -142,6 +142,7 @@ + @@ -163,6 +164,7 @@ + diff --git a/AdventOfCode2024/AdventOfCode2024.vcxproj.filters b/AdventOfCode2024/AdventOfCode2024.vcxproj.filters index 12b5bc2..5ebe69f 100644 --- a/AdventOfCode2024/AdventOfCode2024.vcxproj.filters +++ b/AdventOfCode2024/AdventOfCode2024.vcxproj.filters @@ -75,6 +75,9 @@ Source Files + + Source Files + @@ -140,5 +143,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/AdventOfCode2024/PrintQueue.cpp b/AdventOfCode2024/PrintQueue.cpp new file mode 100644 index 0000000..65bdf9d --- /dev/null +++ b/AdventOfCode2024/PrintQueue.cpp @@ -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 . + +#include +#include + +#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 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]; + } +} diff --git a/AdventOfCode2024/PrintQueue.h b/AdventOfCode2024/PrintQueue.h new file mode 100644 index 0000000..13d2ffa --- /dev/null +++ b/AdventOfCode2024/PrintQueue.h @@ -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 . + +#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); +}; diff --git a/AdventOfCode2024/Program.cpp b/AdventOfCode2024/Program.cpp index a36ee63..c800644 100644 --- a/AdventOfCode2024/Program.cpp +++ b/AdventOfCode2024/Program.cpp @@ -24,6 +24,7 @@ #include "RedNosedReports.h" #include "MullItOver.h" #include "CeresSearch.h" +#include "PrintQueue.h" void Program::run() { @@ -38,6 +39,7 @@ void Program::runSolvers() solverEngine.run(*std::make_unique()); solverEngine.run(*std::make_unique()); solverEngine.run(*std::make_unique()); + solverEngine.run(*std::make_unique()); } std::vector Program::getInputPaths() const