Skip to content

Commit 925affe

Browse files
committed
2025-12-11 part 2 solved with some help
1 parent 6a447d7 commit 925affe

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

2025/csharp/AOC2025.Tests/Day11Tests.cs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ protected override void SolvePart1_Sample()
99
{
1010
var device = x.Split(":", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
1111
return new Device(device[0], device[1].Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
12-
}).ToList();
12+
}).ToArray();
1313

1414
// Act
15-
var pathCount = FindPaths("you", "out", devices.ToArray()).Count;
15+
var pathCount = CountPaths(devices, "you", "out");
1616

1717
// Assert
1818
pathCount.ShouldBe(5);
@@ -25,10 +25,10 @@ protected override void SolvePart1_Actual()
2525
{
2626
var device = x.Split(":", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
2727
return new Device(device[0], device[1].Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
28-
}).ToList();
28+
}).ToArray();
2929

3030
// Act
31-
var pathCount = FindPaths("you", "out", devices.ToArray()).Count;
31+
var pathCount = CountPaths(devices, "you", "out");
3232

3333
// Assert
3434
pathCount.ShouldBe(696);
@@ -41,58 +41,71 @@ protected override void SolvePart2_Sample()
4141
{
4242
var device = x.Split(":", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
4343
return new Device(device[0], device[1].Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
44-
}, partNumber: 2).ToList();
44+
}, partNumber: 2).ToArray();
4545

4646
// Act
47-
var pathCount = FindPaths("svr", "out", devices.ToArray())
48-
.Count(x =>
49-
{
50-
var pathSegments = x.Split(',');
51-
return pathSegments.Contains("dac") && pathSegments.Contains("fft");
52-
});
47+
var svrToDac = CountPaths(devices, "svr", "dac");
48+
var svrToFft = CountPaths(devices, "svr", "fft");
49+
var dacToFft = CountPaths(devices, "dac", "fft");
50+
var fftToDac = CountPaths(devices, "fft", "dac");
51+
var dacToOut = CountPaths(devices, "dac", "out");
52+
var fftToOut = CountPaths(devices, "fft", "out");
53+
54+
var pathCount = svrToDac * dacToFft * fftToOut
55+
+ svrToFft * fftToDac * dacToOut;
5356

5457
// Assert
5558
pathCount.ShouldBe(2);
5659
}
5760

58-
[Ignore("Running way too long...")]
61+
// https://www.reddit.com/r/adventofcode/comments/1pjp1rm/2025_day_11_solutions/
5962
protected override void SolvePart2_Actual()
6063
{
6164
// Arrange
6265
var devices = get_input(x =>
6366
{
6467
var device = x.Split(":", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
6568
return new Device(device[0], device[1].Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
66-
}).ToList();
69+
}).ToArray();
6770

6871
// Act
69-
var paths = FindPaths("svr", "out", devices.ToArray());
70-
var pathCount = paths
71-
.Count(x =>
72-
{
73-
var pathSegments = x.Split(',');
74-
return pathSegments.Contains("dac") && pathSegments.Contains("fft");
75-
});
72+
var svrToDac = CountPaths(devices, "svr", "dac");
73+
var svrToFft = CountPaths(devices, "svr", "fft");
74+
var dacToFft = CountPaths(devices, "dac", "fft");
75+
var fftToDac = CountPaths(devices, "fft", "dac");
76+
var dacToOut = CountPaths(devices, "dac", "out");
77+
var fftToOut = CountPaths(devices, "fft", "out");
78+
79+
var pathCount = svrToDac * dacToFft * fftToOut
80+
+ svrToFft * fftToDac * dacToOut;
7681

7782
// Assert
78-
pathCount.ShouldBe(2);
83+
pathCount.ShouldBe(473741288064360);
7984
}
8085

81-
List<string> FindPaths(string start, string end, Device[] devices, string currentPath = "", List<string>? paths = null)
86+
long CountPaths(Device[] devices, string start, string end, Dictionary<string, long>? cache = null)
8287
{
83-
paths ??= new List<string>();
88+
cache ??= new Dictionary<string, long>();
89+
8490
if (start == end)
8591
{
86-
paths.Add(currentPath);
87-
return paths;
92+
return 1;
8893
}
8994

95+
if (cache.TryGetValue(start, out var paths))
96+
{
97+
return paths;
98+
}
99+
90100
var startDevice = devices.FirstOrDefault(x => x.Id == start);
101+
91102
foreach (var output in startDevice?.Outputs ?? [])
92103
{
93-
paths = FindPaths(output, end, devices, currentPath + "," + output, paths);
104+
paths += CountPaths(devices, output, end, cache);
94105
}
95106

107+
cache[start] = paths;
108+
96109
return paths;
97110
}
98111

0 commit comments

Comments
 (0)