Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day10 #11

Merged
merged 3 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions advent-of-code-2024.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "day09", "day09\day09.csproj
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "day09tests", "tests\day09tests\day09tests.csproj", "{F6CA4DB1-2F70-4418-B5EA-F2063015C7DC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "day10", "day10\day10.csproj", "{A16D82E8-6F58-4A7F-A277-282C534F0BB0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "day10tests", "tests\day10tests\day10tests.csproj", "{DB0D84F3-4032-49C1-96CF-5629E8325CCC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -122,6 +126,14 @@ Global
{F6CA4DB1-2F70-4418-B5EA-F2063015C7DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6CA4DB1-2F70-4418-B5EA-F2063015C7DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6CA4DB1-2F70-4418-B5EA-F2063015C7DC}.Release|Any CPU.Build.0 = Release|Any CPU
{A16D82E8-6F58-4A7F-A277-282C534F0BB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A16D82E8-6F58-4A7F-A277-282C534F0BB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A16D82E8-6F58-4A7F-A277-282C534F0BB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A16D82E8-6F58-4A7F-A277-282C534F0BB0}.Release|Any CPU.Build.0 = Release|Any CPU
{DB0D84F3-4032-49C1-96CF-5629E8325CCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB0D84F3-4032-49C1-96CF-5629E8325CCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB0D84F3-4032-49C1-96CF-5629E8325CCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB0D84F3-4032-49C1-96CF-5629E8325CCC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0F1C1F9C-FDF2-4025-B60E-485EAD0C3905} = {F2656488-9555-4789-A912-089F50725E8D}
Expand All @@ -133,5 +145,6 @@ Global
{AB76B34D-C32C-4825-BD24-CF97D77AAFF1} = {F2656488-9555-4789-A912-089F50725E8D}
{41963400-1A0B-4AD9-9B34-11BA7D415EFD} = {F2656488-9555-4789-A912-089F50725E8D}
{F6CA4DB1-2F70-4418-B5EA-F2063015C7DC} = {F2656488-9555-4789-A912-089F50725E8D}
{DB0D84F3-4032-49C1-96CF-5629E8325CCC} = {F2656488-9555-4789-A912-089F50725E8D}
EndGlobalSection
EndGlobal
156 changes: 156 additions & 0 deletions day10/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
var lines = File.ReadAllLines("input.txt")
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(l => l.ToCharArray().Select(c => (int)char.GetNumericValue(c)).ToArray())
.ToArray();

var map = new TopographicMap(lines);
var trailheadScores = map.CalculateTrailheadScores();
var sumOfTrailheadScores = trailheadScores.Sum();
System.Console.WriteLine($"Sum of trailhead scores: {sumOfTrailheadScores}");

var trailheadRatings = map.CalculateTrailheadRatings();
var sumOfTrailheadRatings = trailheadRatings.Sum();
System.Console.WriteLine($"Sum of trainhead ratings: {sumOfTrailheadRatings}");

public class TopographicMap(int[][] input)
{
private readonly int[][] map = input;

public List<int> CalculateTrailheadScores()
{
List<int> trailheadScores = [];

for (int y = 0; y < map.Length; y++)
{
var trailheadIndex = Array.FindIndex(map[y], x => x == 0);
while (trailheadIndex >= 0)
{
// Depth-First Search from this start position, search for nines
var score = DepthFirstSearch(new Position { X = trailheadIndex, Y = y });
trailheadScores.Add(score);

trailheadIndex = Array.FindIndex(map[y], trailheadIndex + 1, x => x == 0);
}
}

return trailheadScores;
}

public List<int> CalculateTrailheadRatings()
{
List<int> trailheadRatings = [];

for (int y = 0; y < map.Length; y++)
{
var trailheadIndex = Array.FindIndex(map[y], x => x == 0);
while (trailheadIndex >= 0)
{
// Depth-First Search from this start position, search for nines
var score = DepthFirstSearchAllTracks(new Position { X = trailheadIndex, Y = y });
trailheadRatings.Add(score);

trailheadIndex = Array.FindIndex(map[y], trailheadIndex + 1, x => x == 0);
}
}

return trailheadRatings;
}

private int DepthFirstSearch(Position trailhead)
{
var score = 0;
HashSet<Position> discovered = [];
var stack = new Stack<Position>();
stack.Push(trailhead);
while (stack.Count > 0)
{
var position = stack.Pop();
if (discovered.Add(position))
{
if (map[position.Y][position.X] == 9)
{
score++;
continue;
}

// Check all directions for next level
// Up
if (position.Y - 1 >= 0 && map[position.Y - 1][position.X] == map[position.Y][position.X] + 1)
{
stack.Push(new Position { X = position.X, Y = position.Y - 1 });
}

// Right
if (position.X + 1 < map[position.Y].Length && map[position.Y][position.X + 1] == map[position.Y][position.X] + 1)
{
stack.Push(new Position { X = position.X + 1, Y = position.Y });
}

// Down
if (position.Y + 1 < map.Length && map[position.Y + 1][position.X] == map[position.Y][position.X] + 1)
{
stack.Push(new Position { X = position.X, Y = position.Y + 1 });
}

// Left
if (position.X - 1 >= 0 && map[position.Y][position.X - 1] == map[position.Y][position.X] + 1)
{
stack.Push(new Position { X = position.X - 1, Y = position.Y });
}
}
}

return score;
}

private int DepthFirstSearchAllTracks(Position trailhead)
{
var score = 0;
var stack = new Stack<Position>();
stack.Push(trailhead);
while (stack.Count > 0)
{
var position = stack.Pop();

if (map[position.Y][position.X] == 9)
{
score++;
continue;
}

// Check all directions for next level
// Up
if (position.Y - 1 >= 0 && map[position.Y - 1][position.X] == map[position.Y][position.X] + 1)
{
stack.Push(new Position { X = position.X, Y = position.Y - 1 });
}

// Right
if (position.X + 1 < map[position.Y].Length && map[position.Y][position.X + 1] == map[position.Y][position.X] + 1)
{
stack.Push(new Position { X = position.X + 1, Y = position.Y });
}

// Down
if (position.Y + 1 < map.Length && map[position.Y + 1][position.X] == map[position.Y][position.X] + 1)
{
stack.Push(new Position { X = position.X, Y = position.Y + 1 });
}

// Left
if (position.X - 1 >= 0 && map[position.Y][position.X - 1] == map[position.Y][position.X] + 1)
{
stack.Push(new Position { X = position.X - 1, Y = position.Y });
}
}

return score;
}

struct Position
{
public int X;
public int Y;
}
}

10 changes: 10 additions & 0 deletions day10/day10.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
126 changes: 126 additions & 0 deletions tests/day10tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System.Runtime.InteropServices.Marshalling;
using Shouldly;

namespace day10tests;

public class UnitTests
{
[Fact]
public void Test1()
{
string[] lines = [
"...0...",
"...1...",
"...2...",
"6543456",
"7.....7",
"8.....8",
"9.....9",
];
var input = lines.Select(l => l.ToCharArray().Select(c => (int)char.GetNumericValue(c)).ToArray()).ToArray();
var topographicMap = new TopographicMap(input);
var trailheadScores = topographicMap.CalculateTrailheadScores();
trailheadScores.Count.ShouldBe(1);
trailheadScores.Sum().ShouldBe(2);
}

[Fact]
public void Test2()
{
string[] lines = [
"89010123",
"78121874",
"87430965",
"96549874",
"45678903",
"32019012",
"01329801",
"10456732",
];
var input = lines.Select(l => l.ToCharArray().Select(c => (int)char.GetNumericValue(c)).ToArray()).ToArray();
var topographicMap = new TopographicMap(input);
var trailheadScores = topographicMap.CalculateTrailheadScores();
trailheadScores.Count.ShouldBe(9);
trailheadScores.Sum().ShouldBe(36);
}

[Fact]
public void Test3()
{
string[] lines = [
".....0.",
"..4321.",
"..5..2.",
"..6543.",
"..7..4.",
"..8765.",
"..9....",
];

var input = lines.Select(l => l.ToCharArray().Select(c => (int)char.GetNumericValue(c)).ToArray()).ToArray();
var topographicMap = new TopographicMap(input);
var trailheadRatings = topographicMap.CalculateTrailheadRatings();
trailheadRatings.Count.ShouldBe(1);
trailheadRatings.Sum().ShouldBe(3);
}

[Fact]
public void Test4()
{
string[] lines = [
"..90..9",
"...1.98",
"...2..7",
"6543456",
"765.987",
"876....",
"987....",
];

var input = lines.Select(l => l.ToCharArray().Select(c => (int)char.GetNumericValue(c)).ToArray()).ToArray();
var topographicMap = new TopographicMap(input);
var trailheadRatings = topographicMap.CalculateTrailheadRatings();
trailheadRatings.Count.ShouldBe(1);
trailheadRatings.Sum().ShouldBe(13);
}

[Fact]
public void Test5()
{
string[] lines = [
"012345",
"123456",
"234567",
"345678",
"4.6789",
"56789.",
];

var input = lines.Select(l => l.ToCharArray().Select(c => (int)char.GetNumericValue(c)).ToArray()).ToArray();
var topographicMap = new TopographicMap(input);
var trailheadRatings = topographicMap.CalculateTrailheadRatings();
trailheadRatings.Count.ShouldBe(1);
trailheadRatings.Sum().ShouldBe(227);
}

[Fact]
public void Test6()
{
string[] lines = [
"89010123",
"78121874",
"87430965",
"96549874",
"45678903",
"32019012",
"01329801",
"10456732",
];

var input = lines.Select(l => l.ToCharArray().Select(c => (int)char.GetNumericValue(c)).ToArray()).ToArray();
var topographicMap = new TopographicMap(input);
var trailheadRatings = topographicMap.CalculateTrailheadRatings();
trailheadRatings.Count.ShouldBe(9);
trailheadRatings.Sum().ShouldBe(81);
}
}
26 changes: 26 additions & 0 deletions tests/day10tests/day10tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\day10\day10.csproj" />
</ItemGroup>

</Project>
Loading