From e30905c3ab08bbd37b6b5382a5529ddee9e3bded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Sun, 1 Dec 2024 16:32:21 +0100 Subject: [PATCH] Add solutions for "Day 1: Historian Hysteria" --- AdventOfCode2024/AdventOfCode2024.vcxproj | 4 +- .../AdventOfCode2024.vcxproj.filters | 10 +-- AdventOfCode2024/Demo.cpp | 43 ------------ AdventOfCode2024/HistorianHysteria.cpp | 70 +++++++++++++++++++ .../{Demo.h => HistorianHysteria.h} | 7 +- AdventOfCode2024/Program.cpp | 5 +- AdventOfCode2024/UnitTests/UnitTests.cpp | 13 ++-- AdventOfCode2024/UnitTests/UnitTests.vcxproj | 2 +- 8 files changed, 94 insertions(+), 60 deletions(-) delete mode 100644 AdventOfCode2024/Demo.cpp create mode 100644 AdventOfCode2024/HistorianHysteria.cpp rename AdventOfCode2024/{Demo.h => HistorianHysteria.h} (90%) diff --git a/AdventOfCode2024/AdventOfCode2024.vcxproj b/AdventOfCode2024/AdventOfCode2024.vcxproj index cbf78e3..7af98e5 100644 --- a/AdventOfCode2024/AdventOfCode2024.vcxproj +++ b/AdventOfCode2024/AdventOfCode2024.vcxproj @@ -129,13 +129,13 @@ - + - + diff --git a/AdventOfCode2024/AdventOfCode2024.vcxproj.filters b/AdventOfCode2024/AdventOfCode2024.vcxproj.filters index dc84f48..d9e4eb4 100644 --- a/AdventOfCode2024/AdventOfCode2024.vcxproj.filters +++ b/AdventOfCode2024/AdventOfCode2024.vcxproj.filters @@ -18,9 +18,6 @@ Source Files - - Source Files - Source Files @@ -30,6 +27,9 @@ Source Files + + Source Files + @@ -38,10 +38,10 @@ Header Files - + Header Files - + Header Files diff --git a/AdventOfCode2024/Demo.cpp b/AdventOfCode2024/Demo.cpp deleted file mode 100644 index ac5bbb6..0000000 --- a/AdventOfCode2024/Demo.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// 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 "Demo.h" - -std::string Demo::getPuzzleName() const -{ - return "Demo"; -} - -std::string Demo::getInputFileName() const -{ - return "demo.txt"; -} - -void Demo::processDataLine(const std::string& line) -{ - part1 += std::stoi(line); - if (part2 == 0) - { - part2 = std::stoi(line); - } - else - { - part2 *= std::stoi(line); - } -} - -void Demo::finish() -{ -} diff --git a/AdventOfCode2024/HistorianHysteria.cpp b/AdventOfCode2024/HistorianHysteria.cpp new file mode 100644 index 0000000..babad2f --- /dev/null +++ b/AdventOfCode2024/HistorianHysteria.cpp @@ -0,0 +1,70 @@ +// 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 "HistorianHysteria.h" + +std::string HistorianHysteria::getPuzzleName() const +{ + return "Day 1: Historian Hysteria"; +} + +std::string HistorianHysteria::getInputFileName() const +{ + return "historian_hysteria.txt"; +} + +void HistorianHysteria::processDataLine(const std::string& line) +{ + auto pos = line.find(" "); + left.insert(std::stoi(line.substr(0, pos))); + right.insert(std::stoi(line.substr(pos + 3))); +} + +void HistorianHysteria::finish() +{ + int prev{ 0 }; + auto nSame{ 0 }; + auto leftIterator = left.begin(); + auto rightIterator = right.begin(); + auto rightSameIterator = right.begin(); + + while (leftIterator != left.end()) + { + part1 += abs(*leftIterator - *rightIterator); + + if (prev != *leftIterator) + { + nSame = 0; + // Skips over numbers in the right list that are smaller than the current left number. + while (rightSameIterator != right.end() && *rightSameIterator < *leftIterator) + { + rightSameIterator++; + } + // Counts the occurrences of the current left number in the right list. + while (rightSameIterator != right.end() && *rightSameIterator == *leftIterator) + { + rightSameIterator++; + nSame++; + } + prev = *leftIterator; + } + part2 += *leftIterator * nSame; + + leftIterator++; + rightIterator++; + } +} diff --git a/AdventOfCode2024/Demo.h b/AdventOfCode2024/HistorianHysteria.h similarity index 90% rename from AdventOfCode2024/Demo.h rename to AdventOfCode2024/HistorianHysteria.h index d17ae03..218743e 100644 --- a/AdventOfCode2024/Demo.h +++ b/AdventOfCode2024/HistorianHysteria.h @@ -15,9 +15,11 @@ #pragma once +#include + #include "Solver.h" -class Demo : +class HistorianHysteria : public Solver { public: @@ -25,4 +27,7 @@ class Demo : virtual std::string getInputFileName() const override; virtual void processDataLine(const std::string& line) override; virtual void finish() override; +private: + std::multiset left; + std::multiset right; }; diff --git a/AdventOfCode2024/Program.cpp b/AdventOfCode2024/Program.cpp index 83908da..70f0ece 100644 --- a/AdventOfCode2024/Program.cpp +++ b/AdventOfCode2024/Program.cpp @@ -16,10 +16,11 @@ #include #include -#include "Demo.h" #include "Program.h" #include "SolverEngine.h" +#include "HistorianHysteria.h" + void Program::run() { std::cout << "### Advent of Code 2024 ###\n"; @@ -29,7 +30,7 @@ void Program::run() 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/UnitTests/UnitTests.cpp b/AdventOfCode2024/UnitTests/UnitTests.cpp index 62ad50e..d65336a 100644 --- a/AdventOfCode2024/UnitTests/UnitTests.cpp +++ b/AdventOfCode2024/UnitTests/UnitTests.cpp @@ -18,35 +18,36 @@ #include "Part1TestContext.h" #include "Part2TestContext.h" -#include "../Demo.h" #include "../Solver.h" #include "../SolverEngine.h" +#include "../HistorianHysteria.h" + using namespace Microsoft::VisualStudio::CppUnitTestFramework; namespace UnitTests { - TEST_CLASS(DemoTests) + TEST_CLASS(HistorianHysteriaTests) { public: TEST_METHOD(FullData1) { - runTest(*std::make_unique(), 66, part1TestContext_); + runTest(*std::make_unique(), 2176849, part1TestContext_); } TEST_METHOD(FullData2) { - runTest(*std::make_unique(), 39916800, part2TestContext_); + runTest(*std::make_unique(), 23384288, part2TestContext_); } TEST_METHOD(ExampleData1) { - runTest(*std::make_unique(), 10, part1ExampleTestContext_); + runTest(*std::make_unique(), 11, part1ExampleTestContext_); } TEST_METHOD(ExampleData2) { - runTest(*std::make_unique(), 24, part2ExampleTestContext_); + runTest(*std::make_unique(), 31, part2ExampleTestContext_); } private: diff --git a/AdventOfCode2024/UnitTests/UnitTests.vcxproj b/AdventOfCode2024/UnitTests/UnitTests.vcxproj index 7306324..f4b1153 100644 --- a/AdventOfCode2024/UnitTests/UnitTests.vcxproj +++ b/AdventOfCode2024/UnitTests/UnitTests.vcxproj @@ -102,7 +102,7 @@ Windows $(VCInstallDir)UnitTest\lib;..\$(Platform)\$(Configuration);D:\Projects\VS\AdventOfCode2024Full\code\AdventOfCode2024\x64\Debug;%(AdditionalLibraryDirectories) - Demo.obj;Solver.obj;SolverEngine.obj;%(AdditionalDependencies) + Solver.obj;SolverEngine.obj;HistorianHysteria.obj;%(AdditionalDependencies)