-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the solutions for part 1 of days 15 and 16.
- Loading branch information
Showing
15 changed files
with
967 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "maze.h" | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
const auto fileName { "input.txt" }; | ||
|
||
Maze maze { fileName }; | ||
|
||
std::cout << maze.findLowestCostRoute(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?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>17.0</VCProjectVersion> | ||
<Keyword>Win32Proj</Keyword> | ||
<ProjectGuid>{7ece129b-10f5-40b8-8268-3fca13c8b461}</ProjectGuid> | ||
<RootNamespace>My16</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>v143</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v143</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v143</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v143</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> | ||
</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> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<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>stdcpplatest</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>stdcpplatest</LanguageStandard> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClCompile Include="16.cpp" /> | ||
<ClCompile Include="maze.cpp" /> | ||
<ClCompile Include="point2D.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="maze.h" /> | ||
<ClInclude Include="point2D.h" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Text Include="input.txt" /> | ||
<Text Include="test.txt" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?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="16.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
<ClCompile Include="point2D.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
<ClCompile Include="maze.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="point2D.h"> | ||
<Filter>Header Files</Filter> | ||
</ClInclude> | ||
<ClInclude Include="maze.h"> | ||
<Filter>Header Files</Filter> | ||
</ClInclude> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Text Include="input.txt"> | ||
<Filter>Resource Files</Filter> | ||
</Text> | ||
<Text Include="test.txt"> | ||
<Filter>Resource Files</Filter> | ||
</Text> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#include "maze.h" | ||
|
||
#include <fstream> | ||
#include <algorithm> | ||
#include <array> | ||
#include <queue> | ||
#include <unordered_map> | ||
|
||
Maze::Maze(std::string_view fileName) | ||
{ | ||
std::ifstream input { fileName.data() }; | ||
std::string line; | ||
|
||
if ( std::getline( input, line ).good() ) | ||
{ | ||
fSize.fX = line.length(); | ||
fMap.push_back( line ); | ||
} | ||
|
||
int i { 1 }; | ||
while ( std::getline( input, line ).good() && !line.empty() ) | ||
{ | ||
if ( auto j = line.find( 'S' ); j != std::string::npos ) | ||
fStart = { static_cast<int>( j ), i }; | ||
|
||
if ( auto j = line.find( 'E' ); j != std::string::npos ) | ||
fEnd = { static_cast<int>( j ), i }; | ||
|
||
fMap.push_back( line ); | ||
++i; | ||
} | ||
|
||
fSize.fY = fMap.size(); | ||
} | ||
|
||
size_t Maze::findLowestCostRoute() const | ||
{ | ||
std::priority_queue<State, std::vector<State>, std::greater<State>> queue; | ||
std::unordered_map<Point2D, std::unordered_map<std::byte, int>> dist; | ||
|
||
queue.emplace( fStart, Direction::R, 0, Maze::heuristic( fStart, fEnd, Direction::R ) ); | ||
dist[ fStart ][ Direction::R ] = 0; | ||
|
||
while ( !queue.empty() ) | ||
{ | ||
State current { queue.top() }; | ||
queue.pop(); | ||
|
||
if ( current.fPos == fEnd ) | ||
return current.fCost; | ||
|
||
const States neighbours { this->getPossibleNeighbours( current ) }; | ||
|
||
for ( const auto& [newPos, newDir, addCost, heuristic] : neighbours ) | ||
{ | ||
const int newCost { current.fCost + addCost }; | ||
const int newHeuristic { Maze::heuristic( newPos, fEnd, newDir ) }; | ||
|
||
if ( !dist[ newPos ].contains( newDir ) || newCost < dist[ newPos ][ newDir ] ) | ||
{ | ||
dist[ newPos ][ newDir ] = newCost; | ||
queue.emplace( newPos, newDir, newCost, newHeuristic ); | ||
} | ||
} | ||
} | ||
|
||
return -1; // No path found | ||
|
||
} | ||
|
||
Maze::States Maze::getPossibleNeighbours(const State& current) const | ||
{ | ||
static const Point2D dirX { 1, 0 }; | ||
static const Point2D dirY { 0, 1 }; | ||
|
||
static const auto canMoveTo = [] ( char cell ) { return cell == '.' || cell == 'E'; }; | ||
|
||
const Point2D& curr { current.fPos }; | ||
|
||
const Point2D& cellU { curr - dirY }; | ||
const Point2D& cellD { curr + dirY }; | ||
const Point2D& cellL { curr - dirX }; | ||
const Point2D& cellR { curr + dirX }; | ||
|
||
const char charU { fMap[ cellU.fY ][ cellU.fX ] }; | ||
const char charD { fMap[ cellD.fY ][ cellD.fX ] }; | ||
const char charL { fMap[ cellL.fY ][ cellL.fX ] }; | ||
const char charR { fMap[ cellR.fY ][ cellR.fX ] }; | ||
|
||
States possibleMoves; | ||
|
||
if ( current.fDir == Direction::U ) | ||
{ | ||
if ( canMoveTo( charU ) ) | ||
possibleMoves.emplace_back( cellU, Direction::U, 1 ); | ||
|
||
if ( canMoveTo( charL ) ) | ||
possibleMoves.emplace_back( cellL, Direction::L, 1001 ); | ||
|
||
if ( canMoveTo( charR ) ) | ||
possibleMoves.emplace_back( cellR, Direction::R, 1001 ); | ||
} | ||
|
||
if ( current.fDir == Direction::D ) | ||
{ | ||
if ( canMoveTo( charD ) ) | ||
possibleMoves.emplace_back( cellD, Direction::D, 1 ); | ||
|
||
if ( canMoveTo( charR ) ) | ||
possibleMoves.emplace_back( cellR, Direction::R, 1001 ); | ||
|
||
if ( canMoveTo( charL ) ) | ||
possibleMoves.emplace_back( cellL, Direction::L, 1001 ); | ||
} | ||
|
||
if ( current.fDir == Direction::L ) | ||
{ | ||
if ( canMoveTo( charL ) ) | ||
possibleMoves.emplace_back( cellL, Direction::L, 1 ); | ||
|
||
if ( canMoveTo( charD ) ) | ||
possibleMoves.emplace_back( cellD, Direction::D, 1001 ); | ||
|
||
if ( canMoveTo( charU ) ) | ||
possibleMoves.emplace_back( cellU, Direction::U, 1001 ); | ||
} | ||
|
||
if ( current.fDir == Direction::R ) | ||
{ | ||
if ( canMoveTo( charR ) ) | ||
possibleMoves.emplace_back( cellR, Direction::R, 1 ); | ||
|
||
if ( canMoveTo( charU ) ) | ||
possibleMoves.emplace_back( cellU, Direction::U, 1001 ); | ||
|
||
if ( canMoveTo( charD ) ) | ||
possibleMoves.emplace_back( cellD, Direction::D, 1001 ); | ||
} | ||
|
||
return possibleMoves; | ||
} | ||
|
||
int Maze::heuristic(const Point2D& a, const Point2D& b, std::byte dir) | ||
{ | ||
using namespace Direction; | ||
|
||
const int deltaX { std::abs( a.fX - b.fX ) }; | ||
const int deltaY { std::abs( a.fY - b.fY ) }; | ||
const int manhattanDistance { deltaX + deltaY }; | ||
const int turn { deltaX != 0 && deltaY != 0 }; | ||
|
||
return manhattanDistance + turn * 1000; | ||
} |
Oops, something went wrong.