-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
39 lines (32 loc) · 1 KB
/
Program.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
using AdventOfCode.Common;
using Coor = AdventOfCode.Common.Coor<int>;
var map = Resources.GetInputFileLines()
.ParseAsArray(c => c - '0');
var starts = map.AllCoordinates()
.Where(c => map.Get(c) == 0);
Console.WriteLine($"Part 1: {starts.Select(s => GetScore(s, false)).Sum()}");
Console.WriteLine($"Part 2: {starts.Select(s => GetScore(s, true)).Sum()}");
int GetScore(Coor position, bool distinctPaths)
{
return distinctPaths
? GetReachableNines(position).Count
: GetReachableNines(position).Distinct().Count();
}
List<Coor> GetReachableNines(Coor position, List<Coor>? reachableNines = null)
{
reachableNines ??= [];
var target = map.Get(position) + 1;
if (target == 10)
{
reachableNines.Add(position);
return reachableNines;
}
foreach (var n in position.GetFourWayNeighbours())
{
if (n.InBoundsOf(map) && map.Get(n) == target)
{
GetReachableNines(n, reachableNines);
}
}
return reachableNines;
}