-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
72 lines (55 loc) · 1.78 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using AdventOfCode._2021_12;
using AdventOfCode.Common;
var lines = Resources.GetInputFileLines();
var caves = new Dictionary<string, Cave>();
foreach (var line in lines)
{
var parts = line.Split('-');
if (!caves.TryGetValue(parts[0], out var cave1))
{
cave1 = new Cave(parts[0]);
caves.Add(parts[0], cave1);
}
if (!caves.TryGetValue(parts[1], out var cave2))
{
cave2 = new Cave(parts[1]);
caves.Add(parts[1], cave2);
}
if (cave2.Name != "start" && cave1.Name != "end")
{
cave1.Paths.Add(cave2);
}
if (cave1.Name != "start" && cave2.Name != "end")
{
cave2.Paths.Add(cave1);
}
}
static List<List<Cave>> FindAllPaths(Cave from, Cave to, bool allowSmallCaveReentry)
{
return FindPaths([], from, to, allowSmallCaveReentry);
}
static List<List<Cave>> FindPaths(List<Cave> path, Cave current, Cave to, bool allowSmallCaveReentry)
{
if (current == to)
{
return [path/*.Append(to).ToList()*/];
}
path.Add(current);
var paths = new List<List<Cave>>();
var pathLeadsThroughTwoSmalls = path.Any(p => p.IsSmall && path.Count(q => q.Name == p.Name) == 2);
foreach (var cave in current.Paths)
{
if (cave.IsSmall && (pathLeadsThroughTwoSmalls || !allowSmallCaveReentry) && path.Contains(cave))
{
continue;
}
paths.AddRange(FindPaths([.. path], cave, to, allowSmallCaveReentry));
}
return paths;
}
Console.WriteLine($"Part 1: {FindAllPaths(caves["start"], caves["end"], false).Count}");
Console.WriteLine($"Part 2: {FindAllPaths(caves["start"], caves["end"], true).Count}");
//foreach (var path in FindAllPaths(caves["start"], caves["end"], true))
//{
// Console.WriteLine(string.Join(",", path.Select(p => p.Name)));
//}