Solution for Day 1 in C++
This commit is contained in:
130
2025/Cpp/Day01/Day01.cpp
Normal file
130
2025/Cpp/Day01/Day01.cpp
Normal 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();
|
||||
}
|
||||
|
||||
140
2025/Cpp/Day01/Day01.vcxproj
Normal file
140
2025/Cpp/Day01/Day01.vcxproj
Normal 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>
|
||||
22
2025/Cpp/Day01/Day01.vcxproj.filters
Normal file
22
2025/Cpp/Day01/Day01.vcxproj.filters
Normal 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>
|
||||
10
2025/Cpp/Day01/example-input.txt
Normal file
10
2025/Cpp/Day01/example-input.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
4168
2025/Cpp/Day01/puzzle-input.txt
Normal file
4168
2025/Cpp/Day01/puzzle-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user