Solution for Day 1 in C++

This commit is contained in:
Jose Caban
2025-12-01 16:29:09 -05:00
parent 353480c75c
commit 36c0934cc2
10 changed files with 4545 additions and 25 deletions

View File

@@ -1,13 +1,62 @@
#pragma once #pragma once
#include "AdventHelpers/InputFileHelper.h" #include "AdventHelpers/InputFileHelper.h"
#include <iostream>
namespace AdventHelpers namespace AdventHelpers
{ {
class IAdventOfCodeSolution { class AdventOfCodeSolution {
public: public:
virtual void SolveExample(AdventHelper::InputFileHelper& inputFile) = 0; virtual std::string SolveProblem01(AdventHelpers::InputFileHelper& inputFile) = 0;
virtual void SolveProblem01(AdventHelper::InputFileHelper& inputFile) = 0; virtual std::string SolveProblem02(AdventHelpers::InputFileHelper& inputFile) = 0;
virtual void SolveProblem02(AdventHelper::InputFileHelper& inputFile) = 0;
inline AdventHelpers::InputFileHelper* GetExampleInputFile();
inline AdventHelpers::InputFileHelper* GetProblemInputFile();
void SolveAll() {
auto* exampleInput = GetExampleInputFile();
if (!exampleInput) {
std::cerr << "Failed to load example input file." << std::endl;
return;
}
auto* problemInput = GetProblemInputFile();
if (!problemInput) {
std::cerr << "Failed to load problem input file." << std::endl;
AdventHelpers::InputFileHelper::destroy(exampleInput);
return;
}
// Problem 01, example input
std::cout << "Solving problem 01 using example input..." << std::endl;
auto exampleResult01 = SolveProblem01(*exampleInput);
std::cout << "|- Got result: " << exampleResult01 << std::endl;
// Problem 01, puzzle input
std::cout << "Solving problem 01 using puzzle input..." << std::endl;
auto problemResult01 = SolveProblem01(*problemInput);
std::cout << "|- Got result: " << problemResult01 << std::endl;
// Problem 02, example input
std::cout << "Solving problem 02 using example input..." << std::endl;
auto exampleResult02 = SolveProblem02(*exampleInput);
std::cout << "|- Got result: " << exampleResult02 << std::endl;
// Problem 02, puzzle input
std::cout << "Solving problem 02 using puzzle input..." << std::endl;
auto problemResult02 = SolveProblem02(*problemInput);
std::cout << "|- Got result: " << problemResult02 << std::endl;
AdventHelpers::InputFileHelper::destroy(exampleInput);
AdventHelpers::InputFileHelper::destroy(problemInput);
}
}; };
AdventHelpers::InputFileHelper* AdventOfCodeSolution::GetExampleInputFile() {
return AdventHelpers::InputFileHelper::create("example-input.txt");
}
AdventHelpers::InputFileHelper* AdventOfCodeSolution::GetProblemInputFile() {
return AdventHelpers::InputFileHelper::create("puzzle-input.txt");
}
} }

View File

@@ -3,7 +3,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
namespace AdventHelper { namespace AdventHelpers {
// Interface // Interface
class InputFileHelper { class InputFileHelper {
public: public:

View File

@@ -4,7 +4,7 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
namespace AdventHelper { namespace AdventHelpers {
// Opens file, reads each line into mLines vector. Returns null on error. // Opens file, reads each line into mLines vector. Returns null on error.
InputFileHelper* InputFileHelper::create(const char* filename) { InputFileHelper* InputFileHelper::create(const char* filename) {
InputFileHelper* inputFileHelper = nullptr; InputFileHelper* inputFileHelper = nullptr;

View File

@@ -5,32 +5,32 @@
// Interface // Interface
class ExampleSolution : public AdventHelpers::IAdventOfCodeSolution //class ExampleSolution : public AdventHelpers::IAdventOfCodeSolution
{ //{
public: //public:
virtual void SolveExample(AdventHelper::InputFileHelper& inputFile) override; // virtual std::string SolveExample01(AdventHelper::InputFileHelper& inputFile) override;
virtual void SolveProblem01(AdventHelper::InputFileHelper& inputFile) override; // virtual std::string SolveProblem01(AdventHelper::InputFileHelper& inputFile) override;
virtual void SolveProblem02(AdventHelper::InputFileHelper& inputFile) override; // virtual std::string SolveProblem02(AdventHelper::InputFileHelper& inputFile) override;
}; //};
int main() int main()
{ {
auto* inputHelper = AdventHelper::InputFileHelper::create("input.txt"); auto* inputHelper = AdventHelpers::InputFileHelper::create("input.txt");
std::cout << "Hello World!\n"; std::cout << "Hello World!\n";
AdventHelper::InputFileHelper::destroy(inputHelper); AdventHelpers::InputFileHelper::destroy(inputHelper);
} }
// Implementation // Implementation
void ExampleSolution::SolveExample(AdventHelper::InputFileHelper& inputFile) { //void ExampleSolution::SolveExample(AdventHelper::InputFileHelper& inputFile) {
//
} //}
//
void ExampleSolution::SolveProblem01(AdventHelper::InputFileHelper& inputFile) { //void ExampleSolution::SolveProblem01(AdventHelper::InputFileHelper& inputFile) {
//
} //}
//
void ExampleSolution::SolveProblem02(AdventHelper::InputFileHelper& inputFile) { //void ExampleSolution::SolveProblem02(AdventHelper::InputFileHelper& inputFile) {
} //}

View File

@@ -5,4 +5,5 @@
</Configurations> </Configurations>
<Project Path="AdventHelpers/AdventHelpers.vcxproj" Id="df572ffd-18c6-4b8d-8d35-4855f9a05376" /> <Project Path="AdventHelpers/AdventHelpers.vcxproj" Id="df572ffd-18c6-4b8d-8d35-4855f9a05376" />
<Project Path="AdventHelpersTester/AdventHelpersTester.vcxproj" Id="8771a5e2-83fe-497e-8598-18dbf552d6b3" /> <Project Path="AdventHelpersTester/AdventHelpersTester.vcxproj" Id="8771a5e2-83fe-497e-8598-18dbf552d6b3" />
<Project Path="Day01/Day01.vcxproj" Id="ea6fa096-2ae5-4dd8-9834-b032b6ba9e6b" />
</Solution> </Solution>

130
2025/Cpp/Day01/Day01.cpp Normal file
View File

@@ -0,0 +1,130 @@
#include "AdventHelpers/AdventOfCodeSolution.h"
#include <iostream>
#include <cassert>
#include <string>
class Dial {
public:
Dial(bool debug = false, int initialPosition = 50, int minPosition = 0, int maxPosition = 99)
: mDebug(debug)
, mPosition(initialPosition)
, mMinPosition(minPosition)
, mMaxPosition(maxPosition)
{
}
// Returns number of times it passes 0
int TurnLeft(int amount) {
int clicksAtZero = 0;
if (mDebug) std::cout << "Turning left " << amount << " from " << mPosition;
while (amount > 0) {
if (mPosition == 0) {
mPosition = mMaxPosition;
}
else {
mPosition--;
if (mPosition == 0) clicksAtZero++;
}
amount--;
}
assert(amount == 0);
if (mDebug) std::cout << " Ended at: " << mPosition << std::endl;
return clicksAtZero;
}
// Returns number of times it passes 0
int TurnRight(int amount) {
int clicksAtZero = 0;
if (mDebug) std::cout << "Turning right " << amount << " from " << mPosition;
while (amount > 0) {
if (mPosition == mMaxPosition) {
mPosition = 0;
clicksAtZero++;
}
else {
mPosition++;
}
amount--;
}
assert(amount == 0);
if (mDebug) std::cout << " Ended at: " << mPosition << std::endl;
return clicksAtZero;
}
int GetPosition() const {
return mPosition;
}
private:
const bool mDebug;
const int mMaxPosition;
const int mMinPosition;
int mPosition;
};
class Day01Solution : public AdventHelpers::AdventOfCodeSolution
{
public:
virtual std::string SolveProblem01(AdventHelpers::InputFileHelper& inputFile) override {
Dial dial;
int password = 0;
for (const auto& line : inputFile) {
assert(line.length() >= 2);
char direction = line[0];
int amount = std::stoi(line.substr(1));
if (direction == 'L') {
dial.TurnLeft(amount);
}
else if (direction == 'R') {
dial.TurnRight(amount);
}
else {
assert(false && "Invalid direction");
}
if (dial.GetPosition() == 0) {
password++;
}
}
return std::to_string(password);
}
virtual std::string SolveProblem02(AdventHelpers::InputFileHelper& inputFile) override {
Dial dial;
int clicksAtZero = 0;
for (const auto& line : inputFile) {
assert(line.length() >= 2);
char direction = line[0];
int amount = std::stoi(line.substr(1));
if (direction == 'L') {
clicksAtZero += dial.TurnLeft(amount);
}
else if (direction == 'R') {
clicksAtZero += dial.TurnRight(amount);
}
else {
assert(false && "Invalid direction");
}
}
return std::to_string(clicksAtZero);
}
};
int main()
{
Day01Solution solution;
solution.SolveAll();
}

View File

@@ -0,0 +1,140 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>18.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{ea6fa096-2ae5-4dd8-9834-b032b6ba9e6b}</ProjectGuid>
<RootNamespace>Day01</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Day01.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AdventHelpers\AdventHelpers.vcxproj">
<Project>{df572ffd-18c6-4b8d-8d35-4855f9a05376}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Day01.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

File diff suppressed because it is too large Load Diff