diff --git a/2025/Cpp/AdventHelpers/include/AdventHelpers/InputFileHelper.h b/2025/Cpp/AdventHelpers/include/AdventHelpers/InputFileHelper.h index bbc4044..c43e8e8 100644 --- a/2025/Cpp/AdventHelpers/include/AdventHelpers/InputFileHelper.h +++ b/2025/Cpp/AdventHelpers/include/AdventHelpers/InputFileHelper.h @@ -14,12 +14,12 @@ namespace AdventHelpers { inline LinesType::const_iterator begin() const; inline LinesType::const_iterator end() const; + + const char* getLine(size_t index); private: InputFileHelper() = default; ~InputFileHelper() = default; - const char* getLine(); - LinesType mLines; }; diff --git a/2025/Cpp/AdventHelpers/source/AdventOfCodeSolution.cpp b/2025/Cpp/AdventHelpers/source/AdventOfCodeSolution.cpp index c27798a..d70266b 100644 --- a/2025/Cpp/AdventHelpers/source/AdventOfCodeSolution.cpp +++ b/2025/Cpp/AdventHelpers/source/AdventOfCodeSolution.cpp @@ -1,9 +1,13 @@ #include "AdventHelpers/AdventOfCodeSolution.h" +#define ENABLE_EXCEPTION_HANDLING 0 + namespace AdventHelpers { void AdventOfCodeSolution::SolveAll() { +#if ENABLE_EXCEPTION_HANDLING try { +#endif auto* exampleInput = GetExampleInputFile(); if (!exampleInput) { std::cerr << "Failed to load example input file." << std::endl; @@ -39,9 +43,11 @@ namespace AdventHelpers AdventHelpers::InputFileHelper::destroy(exampleInput); AdventHelpers::InputFileHelper::destroy(problemInput); +#if ENABLE_EXCEPTION_HANDLING } catch (...) { std::cerr << "An unexpected error occurred while solving the problems." << std::endl; } +#endif } } // namespace AdventHelpers \ No newline at end of file diff --git a/2025/Cpp/AdventHelpers/source/InputFileHelper.cpp b/2025/Cpp/AdventHelpers/source/InputFileHelper.cpp index 872fbe7..9af386a 100644 --- a/2025/Cpp/AdventHelpers/source/InputFileHelper.cpp +++ b/2025/Cpp/AdventHelpers/source/InputFileHelper.cpp @@ -28,8 +28,8 @@ namespace AdventHelpers { return inputFileHelper; } - const char* InputFileHelper::getLine() { - return nullptr; + const char* InputFileHelper::getLine(size_t index) { + return mLines[index].c_str(); } void InputFileHelper::destroy(InputFileHelper* inputFileHelper) { diff --git a/2025/Cpp/Day02/Day02.cpp b/2025/Cpp/Day02/Day02.cpp index a0696b3..1e52387 100644 --- a/2025/Cpp/Day02/Day02.cpp +++ b/2025/Cpp/Day02/Day02.cpp @@ -3,16 +3,133 @@ #include #include #include +#include +#include + +class Range { +public: + using range_t = long long; + + Range(range_t minValue, range_t maxValue) + : mMinValue(minValue) + , mMaxValue(maxValue) + { + } + + inline range_t left() const { return mMinValue; } + inline range_t right() const { return mMaxValue; } + +private: + const range_t mMinValue; + const range_t mMaxValue; +}; class Day02Solution : public AdventHelpers::AdventOfCodeSolution { public: + using RangeList = std::vector; + + static RangeList ParseLine(std::string line) { + size_t index = 0; + + RangeList ranges; + + while (line.length() > 0) { + std::string left, right; + + size_t start = 0; + size_t leftEnd = line.substr(start).find('-'); + + size_t rightEnd = line.substr(leftEnd).find(','); + if (rightEnd == std::string::npos) rightEnd = line.length() - 1; + + left = line.substr(start, leftEnd); + right = line.substr(leftEnd+1, rightEnd-1); + + ranges.push_back(Range(std::stoll(left), std::stoll(right))); + + line.erase(0, leftEnd + rightEnd + 1); + } + + return ranges; + } + + // An ID is invalid if the first half and the second half match. + // It necessarily must be even. + static bool isValidPart01(const std::string number) { + if (number.length() % 2 == 0) { + const size_t halfLength = number.length() / 2; + + std::string left = number.substr(0, halfLength); + std::string right = number.substr(halfLength); + + if (left.compare(right) == 0) { + return false; + } + } + + return true; + } + + // In part 2, an id is invalid if it has some sequence repeated at least twice. + // 111 = 1 three times, invalid + // 121212 = 12 three times, invalid + // 123123 = same as above, 123 twice + static bool isValidPart02(const std::string number) { + // Once we hit half the length, it must be the same on both sides + const size_t halfLen = number.length() / 2; + + // Start from one character and work up + for (int i = 0; i < halfLen; i++) { + const auto substr = number.substr(0, i + 1); + std::string temp(number); + + while (temp.substr(0, substr.length()).compare(substr) == 0) { + temp.erase(0, substr.length()); + } + + if (temp.length() == 0) return false; + } + + return true; + } + virtual std::string SolveProblem01(AdventHelpers::InputFileHelper& inputFile) override { - return "todo"; + std::string line = inputFile.getLine(0); + + auto ranges = ParseLine(line); + Range::range_t sum = 0; + + for (const auto& range : ranges) { + auto left = range.left(); + auto right = range.right(); + + for (Range::range_t i = left; i <= right; i++) { + if (!isValidPart01(std::to_string(i))) + sum += i; + } + } + + return std::to_string(sum); } virtual std::string SolveProblem02(AdventHelpers::InputFileHelper& inputFile) override { - return "todo"; + std::string line = inputFile.getLine(0); + + auto ranges = ParseLine(line); + Range::range_t sum = 0; + + for (const auto& range : ranges) { + auto left = range.left(); + auto right = range.right(); + + for (Range::range_t i = left; i <= right; i++) { + if (!isValidPart02(std::to_string(i))) + sum += i; + } + } + + return std::to_string(sum); } }; diff --git a/2025/Cpp/Day02/Day02.vcxproj b/2025/Cpp/Day02/Day02.vcxproj index c1e576c..fdefd4c 100644 --- a/2025/Cpp/Day02/Day02.vcxproj +++ b/2025/Cpp/Day02/Day02.vcxproj @@ -79,6 +79,7 @@ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp20 + true Console @@ -94,6 +95,7 @@ WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp20 + true Console @@ -107,6 +109,7 @@ _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp20 + true Console @@ -122,6 +125,7 @@ NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp20 + true Console diff --git a/2025/Cpp/Day02/example-input.txt b/2025/Cpp/Day02/example-input.txt new file mode 100644 index 0000000..bd04584 --- /dev/null +++ b/2025/Cpp/Day02/example-input.txt @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 \ No newline at end of file diff --git a/2025/Cpp/Day02/puzzle-input.txt b/2025/Cpp/Day02/puzzle-input.txt new file mode 100644 index 0000000..3ca85fa --- /dev/null +++ b/2025/Cpp/Day02/puzzle-input.txt @@ -0,0 +1 @@ +3737332285-3737422568,5858547751-5858626020,166911-236630,15329757-15423690,753995-801224,1-20,2180484-2259220,24-47,73630108-73867501,4052222-4199117,9226851880-9226945212,7337-24735,555454-591466,7777695646-7777817695,1070-2489,81504542-81618752,2584-6199,8857860-8922218,979959461-980003045,49-128,109907-161935,53514821-53703445,362278-509285,151-286,625491-681593,7715704912-7715863357,29210-60779,3287787-3395869,501-921,979760-1021259 \ No newline at end of file