010203040506070809101112

Advent of Code

2025/11

Reactor

in C#

by encse

You hear some loud beeping coming from a hatch in the floor of the factory, so you decide to check it out. Inside, you find several large electrical conduits and a ladder.

Climbing down the ladder, you discover the source of the beeping: a large, toroidal reactor which powers the factory above. Some Elves here are hurriedly running between the reactor and a nearby server rack, apparently trying to fix something.

Visit the website for the full story and full puzzle description.

We got an easy dynamic programming exercise for today, a refresher after the last two days of madness. Here is my input rendered to an image.

graph.png

namespace AdventOfCode.Y2025.Day11;

using System;
using System.Collections.Generic;
using System.Linq;

record State(string node, string path);

[ProblemName("Reactor")]
class Solution : Solver {

    public object PartOne(string input) => 
        PathCount(Parse(input), "you", "out", new Dictionary<string, long>());
        
    public object PartTwo(string input) {
        var g = Parse(input);
        return 
            PathCount(g, "svr", "fft", new Dictionary<string, long>()) *
            PathCount(g, "fft", "dac", new Dictionary<string, long>()) *
            PathCount(g, "dac", "out", new Dictionary<string, long>()) +

            PathCount(g, "svr", "dac", new Dictionary<string, long>()) *
            PathCount(g, "dac", "fft", new Dictionary<string, long>()) *
            PathCount(g, "fft", "out", new Dictionary<string, long>());

    }

    long PathCount(
        Dictionary<string, string[]> g,
        string from, string to,
        Dictionary<string, long> cache
    ) {
        if (!cache.ContainsKey(from)) {
            if (from == to) {
                cache[from] = 1;
            } else {
                var res = 0L;
                foreach (var next in g.GetValueOrDefault(from) ?? []) {
                    res += PathCount(g, next, to, cache);
                }
                cache[from] = res;
            }
        }
        return cache[from];
    }
    Dictionary<string, string[]> Parse(string input) => (
        from line in input.Split("\n")
        let parts = line.Split(" ").ToArray()
        let frm = parts[0].TrimEnd(":").ToString()
        let to = parts[1..].ToArray()
        select new KeyValuePair<string, string[]>(frm, to)
    ).ToDictionary();
}

Please ☆ my repo if you like it!

© 2025 | Advent of Code is a registered trademark in the US | Images provided by Bing and Nano Banana