Add solution to day 2
This commit is contained in:
@@ -14,12 +14,12 @@ namespace AdventHelpers {
|
|||||||
|
|
||||||
inline LinesType::const_iterator begin() const;
|
inline LinesType::const_iterator begin() const;
|
||||||
inline LinesType::const_iterator end() const;
|
inline LinesType::const_iterator end() const;
|
||||||
|
|
||||||
|
const char* getLine(size_t index);
|
||||||
private:
|
private:
|
||||||
InputFileHelper() = default;
|
InputFileHelper() = default;
|
||||||
~InputFileHelper() = default;
|
~InputFileHelper() = default;
|
||||||
|
|
||||||
const char* getLine();
|
|
||||||
|
|
||||||
LinesType mLines;
|
LinesType mLines;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
#include "AdventHelpers/AdventOfCodeSolution.h"
|
#include "AdventHelpers/AdventOfCodeSolution.h"
|
||||||
|
|
||||||
|
#define ENABLE_EXCEPTION_HANDLING 0
|
||||||
|
|
||||||
namespace AdventHelpers
|
namespace AdventHelpers
|
||||||
{
|
{
|
||||||
void AdventOfCodeSolution::SolveAll() {
|
void AdventOfCodeSolution::SolveAll() {
|
||||||
|
#if ENABLE_EXCEPTION_HANDLING
|
||||||
try {
|
try {
|
||||||
|
#endif
|
||||||
auto* exampleInput = GetExampleInputFile();
|
auto* exampleInput = GetExampleInputFile();
|
||||||
if (!exampleInput) {
|
if (!exampleInput) {
|
||||||
std::cerr << "Failed to load example input file." << std::endl;
|
std::cerr << "Failed to load example input file." << std::endl;
|
||||||
@@ -39,9 +43,11 @@ namespace AdventHelpers
|
|||||||
|
|
||||||
AdventHelpers::InputFileHelper::destroy(exampleInput);
|
AdventHelpers::InputFileHelper::destroy(exampleInput);
|
||||||
AdventHelpers::InputFileHelper::destroy(problemInput);
|
AdventHelpers::InputFileHelper::destroy(problemInput);
|
||||||
|
#if ENABLE_EXCEPTION_HANDLING
|
||||||
}
|
}
|
||||||
catch (...) {
|
catch (...) {
|
||||||
std::cerr << "An unexpected error occurred while solving the problems." << std::endl;
|
std::cerr << "An unexpected error occurred while solving the problems." << std::endl;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
} // namespace AdventHelpers
|
} // namespace AdventHelpers
|
||||||
@@ -28,8 +28,8 @@ namespace AdventHelpers {
|
|||||||
return inputFileHelper;
|
return inputFileHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* InputFileHelper::getLine() {
|
const char* InputFileHelper::getLine(size_t index) {
|
||||||
return nullptr;
|
return mLines[index].c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputFileHelper::destroy(InputFileHelper* inputFileHelper) {
|
void InputFileHelper::destroy(InputFileHelper* inputFileHelper) {
|
||||||
|
|||||||
@@ -3,16 +3,133 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <string>
|
#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
|
class Day02Solution : public AdventHelpers::AdventOfCodeSolution
|
||||||
{
|
{
|
||||||
public:
|
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 {
|
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 {
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -79,6 +79,7 @@
|
|||||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
|
<OpenMPSupport>true</OpenMPSupport>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
@@ -94,6 +95,7 @@
|
|||||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
|
<OpenMPSupport>true</OpenMPSupport>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
@@ -107,6 +109,7 @@
|
|||||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
|
<OpenMPSupport>true</OpenMPSupport>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
@@ -122,6 +125,7 @@
|
|||||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
|
<OpenMPSupport>true</OpenMPSupport>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
|
|||||||
1
2025/Cpp/Day02/example-input.txt
Normal file
1
2025/Cpp/Day02/example-input.txt
Normal 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
|
||||||
1
2025/Cpp/Day02/puzzle-input.txt
Normal file
1
2025/Cpp/Day02/puzzle-input.txt
Normal 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
|
||||||
Reference in New Issue
Block a user