1
0
Fork 0

Add Day 10

This commit is contained in:
Jöran Malek 2024-12-26 18:15:31 +01:00
parent 4816b640bf
commit 3ad5ad40c0
4 changed files with 140 additions and 1 deletions

View file

@ -1,4 +1,4 @@

Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59 VisualStudioVersion = 17.0.31903.59
@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "core", "src\core\core.cspro
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-09", "src\2024\09\aoc-2024-09.csproj", "{BFF5A025-BCA5-426C-AA21-237E1059AEA7}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-09", "src\2024\09\aoc-2024-09.csproj", "{BFF5A025-BCA5-426C-AA21-237E1059AEA7}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-10", "src\2024\10\aoc-2024-10.csproj", "{295CA1CA-AF90-41EC-A077-98D20A00A6AF}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -74,6 +76,10 @@ Global
{BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Debug|Any CPU.Build.0 = Debug|Any CPU {BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Release|Any CPU.ActiveCfg = Release|Any CPU {BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Release|Any CPU.Build.0 = Release|Any CPU {BFF5A025-BCA5-426C-AA21-237E1059AEA7}.Release|Any CPU.Build.0 = Release|Any CPU
{295CA1CA-AF90-41EC-A077-98D20A00A6AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{295CA1CA-AF90-41EC-A077-98D20A00A6AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{295CA1CA-AF90-41EC-A077-98D20A00A6AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{295CA1CA-AF90-41EC-A077-98D20A00A6AF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{FAF70800-E2C3-4AD7-B433-86C1F20380F7} = {37CA8017-085A-4F6A-BADB-535F929C10B9} {FAF70800-E2C3-4AD7-B433-86C1F20380F7} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
@ -85,5 +91,6 @@ Global
{58941BB2-E4AC-40F0-AA92-BEF51DEFC859} = {37CA8017-085A-4F6A-BADB-535F929C10B9} {58941BB2-E4AC-40F0-AA92-BEF51DEFC859} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
{44BBC1CF-D2FC-4906-9691-B439EC9EFB8C} = {37CA8017-085A-4F6A-BADB-535F929C10B9} {44BBC1CF-D2FC-4906-9691-B439EC9EFB8C} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
{BFF5A025-BCA5-426C-AA21-237E1059AEA7} = {37CA8017-085A-4F6A-BADB-535F929C10B9} {BFF5A025-BCA5-426C-AA21-237E1059AEA7} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
{295CA1CA-AF90-41EC-A077-98D20A00A6AF} = {37CA8017-085A-4F6A-BADB-535F929C10B9}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

88
src/2024/10/Program.cs Normal file
View file

@ -0,0 +1,88 @@
using System.Collections;
using System.Collections.Frozen;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Xml;
using ConsoleAppFramework;
using core;
var builder = ConsoleApp.Create();
builder.Add("part-one", static ([Argument, FileExists] string file) =>
{
Console.WriteLine($"Result: {ReadFile(file, true)}");
});
builder.Add("part-two", static ([Argument, FileExists] string file) =>
{
Console.WriteLine($"Result: {ReadFile(file, false)}");
});
builder.Run(args);
static long ReadFile(string file, bool unique)
{
SymbolGrid grid = new();
grid.Load(file);
ScenarioMap map = new(grid);
HashSet<Vector2I> startingPositions = [];
for (int x = 0; x < map.Width; x++)
{
for (int y = 0; y < map.Height; y++)
{
if (map[x, y] == 0)
{
startingPositions.Add(new(x, y));
}
}
}
long result = 0;
foreach (var position in startingPositions)
{
ICollection<Vector2I> target = unique ? new HashSet<Vector2I>() : new List<Vector2I>();
Walk(map, position, target);
result += target.Count;
Console.WriteLine(target.Count);
}
return result;
static void Walk(ScenarioMap map, Vector2I currentPosition, ICollection<Vector2I> results)
{
var height = map[currentPosition];
if (height == 9)
{
results.Add(currentPosition);
return;
}
foreach (var d in s_directions.Span)
{
var newPosition = currentPosition + d;
if (!map.IsValid(newPosition))
{
continue;
}
if ((map[newPosition] - height) != 1)
{
continue;
}
Walk(map, newPosition, results);
}
}
}
partial class Program
{
static ReadOnlyMemory<Vector2I> s_directions;
static Program()
{
s_directions = (Vector2I[])[new(-1, 0), new(1, 0), new(0, -1), new(0, 1)];
}
}

View file

@ -0,0 +1,29 @@
using core;
record class ScenarioMap
{
private readonly int[] _heights;
public int this[Vector2I position] => this[position.X, position.Y];
public int this[int x, int y] => _heights[y * Width + x];
public int Width { get; }
public int Height { get; }
public ScenarioMap(SymbolGrid grid)
{
(Width, Height) = (grid.Width, grid.Height);
_heights = new int[Width * Height];
foreach (var (position, symbol) in grid)
{
_heights[position.Y * Width + position.X] = symbol - '0';
}
}
public bool IsValid(Vector2I position) => IsValid(position.X, position.Y);
public bool IsValid(int x, int y) => x >= 0 && x < Width && y >= 0 && y < Height;
}

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>aoc_2024_10</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\core\core.csproj" />
</ItemGroup>
</Project>