-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12Part2.cs
46 lines (38 loc) · 1.32 KB
/
Day12Part2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using AdventOfCode.Common;
using Microsoft.Extensions.Logging;
namespace AdventOfCode.Day12;
[Solution("Day12", "Part2")]
public class Day12Part2 : Day12
{
private readonly ILogger<Day12Part2> _logger;
public Day12Part2(ILogger<Day12Part2> logger) => _logger = logger;
protected override void RunDay12(Grid grid)
{
// Find all potential starting points (elevation == 0 / 'a')
var startingPoints = FindStartingPoints(grid);
// Find the shortest path from any of the starting points
var shortestPathLength = int.MaxValue;
foreach (var startingPoint in startingPoints)
{
var distance = CountDistanceToEnd(grid, startingPoint);
if (distance < shortestPathLength)
{
shortestPathLength = distance;
}
}
_logger.LogInformation("The shortest path requires [{num}] steps.", shortestPathLength);
}
private static IEnumerable<Point> FindStartingPoints(Grid grid)
{
for (var row = 0; row < grid.Height; row++)
{
for (var col = 0; col < grid.Width; col++)
{
if (grid[row, col].Elevation == 0)
{
yield return new Point(row, col);
}
}
}
}
}