-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.cs
57 lines (49 loc) · 1.7 KB
/
Day10.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
47
48
49
50
51
52
53
54
55
56
57
using Aoc2024Net.Utilities;
using Trail = System.Collections.Generic.List<Aoc2024Net.Utilities.Coordinate>;
namespace Aoc2024Net.Days
{
internal sealed class Day10 : Day
{
private static readonly Coordinate[] Moves =
{
new(-1, 0),
new(1, 0),
new(0, -1),
new(0, 1),
};
public override object? SolvePart1() => GetTrails()
.GroupBy(t => t.First())
.Sum(g => g.Select(t => t.Last()).Distinct().Count());
public override object? SolvePart2() => GetTrails()
.Count();
private IEnumerable<Trail> GetTrails()
{
var (grid, width, height) = InputData.GetInputInt32Grid(0, ' ');
var trailHeads = grid
.GetAllCoordinates()
.Where(c => grid.At(c) == 0)
.ToArray();
foreach (var trailHead in trailHeads)
{
var queue = new Queue<Trail>();
queue.Enqueue([trailHead]);
while (queue.Count != 0)
{
var trail = queue.Dequeue();
if (trail.Count == 10)
yield return trail;
var nextPoints = Moves
.Select(m => trail.Last() + m)
.Where(c =>
grid.IsInGrid(width, height, c) &&
grid.At(c) == grid.At(trail.Last()) + 1)
.ToArray();
foreach (var point in nextPoints)
{
queue.Enqueue([.. trail, point]);
}
}
}
}
}
}