Skip to content

Commit db12572

Browse files
Merge pull request #36 from MartinZikmund/feature/aoc2025-day11
2 parents 7f98b05 + 96f552e commit db12572

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace AdventOfCode.Puzzles._2025._11.Part1;
2+
3+
public class Solution : IPuzzleSolution
4+
{
5+
private Dictionary<string, string[]> _targets;
6+
7+
public async Task<string> SolveAsync(StreamReader inputReader)
8+
{
9+
var lines = await inputReader.ReadAllLinesAsync();
10+
11+
12+
_targets = new Dictionary<string, string[]>();
13+
foreach (var line in lines)
14+
{
15+
var parts = line.Split(": ");
16+
var key = parts[0];
17+
var values = parts[1].Split(" ", StringSplitOptions.TrimEntries).ToArray();
18+
_targets[key] = values;
19+
}
20+
21+
return CountPaths("you").ToString();
22+
}
23+
24+
private int CountPaths(string current)
25+
{
26+
if (current == "out")
27+
{
28+
return 1;
29+
}
30+
31+
var targets = _targets[current];
32+
var total = 0;
33+
foreach (var target in targets)
34+
{
35+
total += CountPaths(target);
36+
}
37+
38+
return total;
39+
}
40+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace AdventOfCode.Puzzles._2025._11.Part2;
4+
5+
public class Solution : IPuzzleSolution
6+
{
7+
private Dictionary<string, string[]> _targets;
8+
private Dictionary<(string node, bool fft, bool dac), ulong> _pathCounts = new();
9+
10+
public async Task<string> SolveAsync(StreamReader inputReader)
11+
{
12+
var lines = await inputReader.ReadAllLinesAsync();
13+
14+
15+
_targets = new Dictionary<string, string[]>();
16+
foreach (var line in lines)
17+
{
18+
var parts = line.Split(": ");
19+
var key = parts[0];
20+
var values = parts[1].Split(" ", StringSplitOptions.TrimEntries).ToArray();
21+
_targets[key] = values;
22+
}
23+
24+
var visited = new HashSet<string>();
25+
visited.Add("svr");
26+
return CountPaths("svr", false, false, visited).ToString();
27+
}
28+
29+
private ulong CountPaths(string current, bool fft, bool dac, HashSet<string> visited)
30+
{
31+
if (current == "dac")
32+
{
33+
dac = true;
34+
}
35+
36+
if (current == "fft")
37+
{
38+
fft = true;
39+
}
40+
41+
if (current == "out")
42+
{
43+
_pathCounts[("out", fft, dac)] = fft && dac ? 1UL : 0UL;
44+
return _pathCounts[("out", fft, dac)];
45+
}
46+
47+
if (!_targets.ContainsKey(current))
48+
{
49+
return 0;
50+
}
51+
52+
var targets = _targets[current];
53+
var total = 0UL;
54+
55+
foreach (var target in targets.Where(t => !visited.Contains(t)))
56+
{
57+
visited.Add(target);
58+
if (_pathCounts.TryGetValue((target, fft, dac), out var cachedPaths))
59+
{
60+
total += cachedPaths;
61+
}
62+
else
63+
{
64+
var pathsForNode = CountPaths(target, fft, dac, visited);
65+
_pathCounts[(target, fft, dac)] = pathsForNode;
66+
total += pathsForNode;
67+
}
68+
visited.Remove(target);
69+
}
70+
71+
return total;
72+
}
73+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
svr: aaa bbb
2+
aaa: fft
3+
fft: ccc
4+
bbb: tty
5+
tty: ccc
6+
ccc: ddd eee
7+
ddd: hub
8+
hub: fff
9+
eee: dac
10+
dac: fff
11+
fff: ggg hhh
12+
ggg: out
13+
hhh: out
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
aaa: you hhh
2+
you: bbb ccc
3+
bbb: ddd eee
4+
ccc: ddd eee fff
5+
ddd: ggg
6+
eee: out
7+
fff: out
8+
ggg: out
9+
hhh: ccc fff iii
10+
iii: out

0 commit comments

Comments
 (0)