diff --git a/AdventOfCode.sln b/AdventOfCode.sln index 87a24f2..f06f72b 100644 --- a/AdventOfCode.sln +++ b/AdventOfCode.sln @@ -1,4 +1,4 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "core", "src\core\core.cspro EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-09", "src\2024\09\aoc-2024-09.csproj", "{BFF5A025-BCA5-426C-AA21-237E1059AEA7}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoc-2024-10", "src\2024\10\aoc-2024-10.csproj", "{295CA1CA-AF90-41EC-A077-98D20A00A6AF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(NestedProjects) = preSolution {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} {44BBC1CF-D2FC-4906-9691-B439EC9EFB8C} = {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 EndGlobal diff --git a/src/2024/10/Program.cs b/src/2024/10/Program.cs new file mode 100644 index 0000000..97ebc6e --- /dev/null +++ b/src/2024/10/Program.cs @@ -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 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 target = unique ? new HashSet() : new List(); + Walk(map, position, target); + result += target.Count; + Console.WriteLine(target.Count); + } + + return result; + + static void Walk(ScenarioMap map, Vector2I currentPosition, ICollection 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 s_directions; + + static Program() + { + s_directions = (Vector2I[])[new(-1, 0), new(1, 0), new(0, -1), new(0, 1)]; + } +} diff --git a/src/2024/10/ScenarioMap.cs b/src/2024/10/ScenarioMap.cs new file mode 100644 index 0000000..8b47479 --- /dev/null +++ b/src/2024/10/ScenarioMap.cs @@ -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; +} diff --git a/src/2024/10/aoc-2024-10.csproj b/src/2024/10/aoc-2024-10.csproj new file mode 100644 index 0000000..839e7a0 --- /dev/null +++ b/src/2024/10/aoc-2024-10.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + aoc_2024_10 + enable + enable + + + + + + + \ No newline at end of file