Skip to content

Commit

Permalink
2024 Day10 Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 10, 2024
1 parent daabf0e commit 1ffcce0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="AngleSharp" Version="1.1.2" />
<PackageVersion Include="Smab.Helpers" Version="1.7.16" />
<PackageVersion Include="Smab.Helpers" Version="1.7.17" />
</ItemGroup>
</Project>
64 changes: 25 additions & 39 deletions Solutions/2024/Day10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,56 @@
[Description("Hoof It")]
public static partial class Day10 {

private static int[,] _lavaMap = default!;
private static List<Trail> _allRoutes = [];

[Init]
public static void LoadLavaMap(string[] input)
public static void FindAllTheTrails(string[] input)
{
char[,] lavaMapTemp = input.Select(i => i).To2dArray();
_lavaMap = new int[lavaMapTemp.ColsCount(), lavaMapTemp.RowsCount()];

foreach (Cell<char> cell in lavaMapTemp.ForEachCell()) {
if (char.IsAsciiDigit(cell)) {
_lavaMap[cell.Col, cell.Row] = cell.Value.ToString().As<int>();
} else {
_lavaMap[cell.Col, cell.Row] = int.MinValue;
}
}
int[,] lavaMap = input.Select(i => i.AsHeights()).ToList().To2dArray();

_allRoutes = [..
lavaMap
.ForEachCell()
.Where(location => location.Value is 0)
.SelectMany(th => lavaMap
.HikeToSummit(new(0, 0, th.Index, th.Index, [th.Index])))
];
}

public static int Part1(string[] _)
{
var result = _lavaMap.
ForEachCell()
.Where(location => location.Value is 0)
.SelectMany(th => _lavaMap
.HikeToSummit(new(0, 0, th.Index, th.Index, [th.Index]), Directions.AllDirections))
.ToList()
.Where(trail => trail.End == SUMMIT)
return _allRoutes
.Select(trail => (trail.StartPoint, trail.EndPoint))
.Distinct()
.Count()
;

return result;
.Count();
}

private static List<Trail> HikeToSummit(this int[,] map, Trail trail, IEnumerable<Direction> allDirections)
{
List<Trail> trails = [];
public static int Part2(string[] _) => _allRoutes.Count;

foreach (Direction direction in allDirections) {
private static IEnumerable<Trail> HikeToSummit(this int[,] map, Trail trail)
{
foreach (Direction direction in Directions.AllDirections) {
Point nextPoint = trail.EndPoint + direction.Delta();
if (map.TryGetValue(nextPoint, out int height)) {
if (height == trail.End + 1) {
Trail nextTrail = trail with { EndPoint = nextPoint, End = height };
Trail nextTrail = trail with { EndPoint = nextPoint, End = height, Route = [..trail.Route, nextPoint] };
if (height is SUMMIT) {
trails.Add(nextTrail);
yield return nextTrail;
continue;
} else {
List<Trail> newTrails = [.. map.HikeToSummit(nextTrail, allDirections)];
foreach (Trail newTrail in newTrails) {
if (newTrail.End is SUMMIT) {
trails.Add( newTrail);
}
}

List<Trail> newTrails = [.. map.HikeToSummit(nextTrail)];
foreach (Trail newTrail in newTrails) {
if (newTrail.End is SUMMIT) {
yield return newTrail;
}
}
}
}
}

return trails;

}

public static string Part2(string[] input, params object[]? args) => NO_SOLUTION_WRITTEN_MESSAGE;
private static List<int> AsHeights(this string s) => [..s.Select(x => char.IsAsciiDigit(x) ? int.Parse($"{x}", null) : int.MinValue)];

public record Trail(int Start, int End, Point StartPoint, Point EndPoint, List<Point> Route);

Expand Down
47 changes: 43 additions & 4 deletions Tests/2024/Tests_10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ public class Tests_10_Hoof_It
{
const int DAY = 10;

private const string TEST_DATA = """
2333133121414131402
""";

[Theory]
[InlineData("""
...0...
Expand Down Expand Up @@ -51,4 +47,47 @@ public void Part1(string input, int expected)
_ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART1, input), out int actual);
actual.ShouldBe(expected);
}

[Theory]
[InlineData("""
.....0.
..4321.
..5..2.
..6543.
..7..4.
..8765.
..9....
""", 3)]
[InlineData("""
..90..9
...1.98
...2..7
6543456
765.987
876....
987....
""", 13)]
[InlineData("""
012345
123456
234567
345678
4.6789
56789.
""", 227)]
[InlineData("""
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
""", 81)]
public void Part2(string input, int expected)
{
_ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input), out int actual);
actual.ShouldBe(expected);
}
}

0 comments on commit 1ffcce0

Please sign in to comment.