Advent of Code Day 11: Connecting the Reactor With a Server Rack

On Day 11 of Advent of Code, we’re helping to connect a reactor with a server rack by following the path between devices.

Day 11 made me feel like an impostor. Since it involved a graph (one device outputs to multiple devices), I imagined a GetAllPaths() function and a LINQ query to count paths ending with “out.”

After more than 2 hours struggling with a recursive function, I looked at others’ solutions and realized I was overcomplicating it.

Yes, the solution used recursion. But it was simpler than I thought.

Here’s my full solution,

var connections = new Dictionary<string, IEnumerable<string>>
{
    { "aaa", [  "you", "hhh" ] },
    { "you", [  "bbb", "ccc" ] },
    { "bbb", [  "ddd", "eee" ] },
    { "ccc", [  "ddd", "eee", "fff" ] },
    { "ddd", [  "ggg" ] },
    { "eee", [  "out" ] },
    { "fff", [  "out" ] },
    { "ggg", [  "out" ] },
    { "hhh", [  "ccc", "fff", "iii" ] },
    { "iii", [  "out" ] },
};
var paths = CountPaths(connections, "you", "out");
Console.WriteLine(paths);
Console.ReadKey();

static int CountPaths(Dictionary<string, IEnumerable<string>> connections, string starting, string ending)
{
    if (starting == ending) return 1;

    var paths = 0;
    foreach (var connection in connections.GetValueOrDefault(starting, []))
    {
        paths += CountPaths(connections, connection, ending);
    }

    return paths;
}

Et voilà! Barely. Most people found today a warm-up after yesterday’s puzzle. But for me, it was the harder day. Trying to be too functional made me overcomplicate everything. Simple always wins, especially for Advent of Code.

Advent of Code sharpens your coding skills. But coding is more than typing symbols fast. It’s also about teamwork, collaboration, and many skills I share in my book, Street-Smart Coding: 30 Ways to Get Better at Coding. That’s the roadmap I wish I’d known from day one.

Get your copy of Street-Smart Coding here