Add solution to day 2

This commit is contained in:
Jose Caban
2025-12-02 01:12:59 -05:00
parent b2c1ba8227
commit 3e35b16388
7 changed files with 135 additions and 6 deletions

View File

@@ -3,16 +3,133 @@
#include <iostream>
#include <cassert>
#include <string>
#include <cstring>
#include <sstream>
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<Range>;
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);
}
};

View File

@@ -79,6 +79,7 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -94,6 +95,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -107,6 +109,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -122,6 +125,7 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

View File

@@ -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

View File

@@ -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