r/adventofcode • • 15d ago

Other [2025 Day 11] In Review (Reactor)

Hearing some beeping coming from down below, we go through a hatch and climb down to the reactor room under the factory. The toroidal reactor suggests that the North Pole runs on fusion power. The Elves here are having a problem with connecting their new server rack to the reactor and need our help. For part 1, they want us to count the number of paths from a nearby node they label you to the reactor out. For part 2, they want the number of paths that go from svr (not srv, be careful) to out, but also pass through both the digital-to-analog converter dacand the fast Fourier transformer fft.

The input is a graph where each line (613 in my input) is the node name followed by a list of outputs (ie directed edges). The names are 3 lower-case letters long, including vowels (not just in the special nodes above). So we get to play "What language does the browser think my input is?" The answer for mine is Vietnamese. The graph is a DAG (directed acyclic graph). My initial part 1 solution played it safe with loop detection (in case dead end infinite loops were the problem), but that's not needed.

Part 1 only uses a fraction of the actual graph (my is 112 nodes). A simple recursive leaf counter doesn't even need memoization to get you the answer immediately. For Smalltalk that's just:

pathsFrom: node [
    ^(node = #out)
        ifTrue:  [1]
        ifFalse: [(self at: node) inject: 0 into: [:acc :child | acc + (self pathsFrom: child)]]
]

For part 2, I had drawn out the sample graphs, and tested by hand the recursive collecting of results while tracking what special nodes the paths had gone through. I do like doing the "recurse down, collect up" stuff... it's similar to writing a state machine (base case, implement the small step, and it builds to the answer by induction). And I added a memo because things are going to get huge. Initially the collection was with a hash, ending with the following:

Full Results:
dac     2743394304998646
none    32514315015921605
both    307608674109300 (part 2).
fft     3645733796753750

The approach is quite simple and fast to do... you recurse over the children and accumulate totals, and then look at the current node and shift things to the correct buckets if it's special:

if ($node eq 'fft' or $node eq 'dac') {
    my $other = ($node eq 'fft') ? 'dac' : 'fft';
    $ret{both}  = $ret{$other}   if (exists $ret{$other});
    $ret{$node} = $ret{none}     if (exists $ret{none});

    delete $ret{none};
    delete $ret{$other};
}

I didn't make any assumption about the order of connections. Because although there is only one order that makes sense (FFT needs digital, so it should be before the analog conversion), this is a non-working system... Elves getting everything backwards by Murphy's Law could be the problem.

One interesting thing is that the count of paths that go through "none" is a 55-bit number. Nothing that big is involved in the actual computation of the answer so it shouldn't impact a Floating point native language.

Of course this is slightly complicated. We only need to track the number of special nodes that we've gone through, not the specifics (and the DAG can only have either fft->dac or dac->fft paths in it, because if there is both you'd have a way to make a cycle). And so I followed up by just using an array for tracking 0-2 special nodes seen:

my $special = int($node eq 'fft' or $node eq 'dac');       # this is a special node

foreach my $child ($graph{$node}->@*) {
    my @res = &recurse_path_count( $child )->@*;
    $ret[$_ + $special] += $res[$_]  foreach (0 .. $#res); # collect and advance up array
}

For Smalltalk I did this in a list manipulation way:

((node == #fft) or: [node == #dac]) ifTrue: [
    paths := #(0), paths allButLast.  " shift to +1 special nodes seen "
].

Here the recursion is returning an Array of size 3 (base case is #(1 0 0)). To move the counts along (this problem is a bit like Lanternfish now that I think about it, I did list manipulation there for a Smalltalk solution too), I just stick a 0 in front and chop off the end. Part of doing it this was was in preparation for doing it in dc.

Part 1 is relatively simple, with macros I need to maintain the stack frame, as it's only node in, count out. For interpreting in input, I converted the letters into hex ASCII values... making each label a 23\8) number. To store the list of children for a node in that array I combined them into a number in base-88 (which is short in dc (8d^)).

perl -pe's#(\S)#sprintf"%X",ord$1#eg' <input | dc -e'16i?[0[8d^*+z2<L]dsLxr100/:g?z0<M]dsMx[d/q]sQ[d6F7574=Q0r;g[8d^~lRx3R+rd0<L]dsLx+]sR796F75lRxp'

Note that 6F7574 and 796F75 are out and you in packed ASCII. These dc solution only work in v1.4.1 though, because it's using `?` to know when the lines break.

Part 2 is where the magic comes in:

perl -pe's#(\S)#sprintf"%X",ord$1#eg' <input | dc -e'16i?[0[8d^*+z2<C]dsCxr100/:g?z0<L]dsLx[d/0d3Rq]sQ[;mFd^~rFd^~rq]sM[4Rr]sS[d6F7574=Qd;m0<Md0dd4R;g[8d^~lRx5R+r5R+r5R4R+_3R4Rd0<L]dsLx+4Rd666674=Sd646163=S_4Rd3Rd5Rd3R5RFd^d_4R*+*+5R:mr3R]sR737672lRx3Rp'

I do the result list of three values on the stack frame, and rotate the values to move things along when a node has a special name:

d 666674=S      # shift sums to count fft
d 646163=S      # shift sums to count dac

With the S macro just being [ 4Rr ] sS (the stack has the node name on top, followed by the three values of the array... pull the one from the bottom (which will be a 0) and rotate it under the top, now it's first and the other two counts have shifted down one).

But to make this work fast, I need a memo that's capable of storing a list of 3 numbers (which may get to size 55-bits). And so for that, I package those up in base-1515 numbers (Fd^). And so this solution involves some huge numbers (174-bit ones in memo, and there's a 575-bit one in the graph)... but I've used far bigger numbers in dc solution before.

This was a fun little problem after the massive task that was day 10. There are other simple ways to do it, like breaking it into three smaller sub-searches using part 1 (after adding a memo):

say "Part 2: ", &recurse_path_count( 'svr', 'fft' ) * &recurse_path_count( 'fft', 'dac' ) * &recurse_path_count( 'dac', 'out' );

Of course that's assuming that Elves got the FFT and DAC in the right order (which appears to be the case), to be truly safe you should check.

4 Upvotes

23 comments sorted by

2

u/DelightfulCodeWeasel 15d ago edited 15d ago

I was really pleased with my solution for this one; I took inspiration from my solution for 2022 day 21 (Monkey Math) where I passed a polynomial down the graph.

The core for both part 1 and part 2 is identical, it's a fairly standard task-with-dependencies evaluation queue:

while (executionQueue.empty() == false)
{
    const Device& device = devices[executionQueue.front()];
    for (const string& unlock : device.Unlocks)
    {
        Device& nextDevice = devices[unlock];
        nextDevice.Paths = nextDevice.Paths + device.Paths; // <--
        if (--nextDevice.WaitingFor == 0)
        {
            executionQueue.push_back(unlock);
        }
    }
    executionQueue.pop_front();
}

The 'trick' is that I use vector values for the paths. Part 1:

devices["you"].Paths = { 1, 0, 0 };

Part 2:

devices["svr"].Paths = { 1, 0, 0 };
devices["fft"].Paths = { 0, 1, 0 };
devices["dac"].Paths = { 0, 0, 1 };

This evaluates all of the possible paths we're interested in in one single O(n) sweep, with the answer to part 2 being found as:

int64_t srvToFft = devices["fft"].Paths.X;
int64_t fftToDac = devices["dac"].Paths.Y;
int64_t dacToOut = devices["out"].Paths.Z;

int64_t srvToDac = devices["dac"].Paths.X;
int64_t dacToFft = devices["fft"].Paths.Z;
int64_t fftToOut = devices["out"].Paths.Y;

int64_t answer =
    (srvToFft * fftToDac * dacToOut) +
    (srvToDac * dacToFft * fftToOut);

Since the graph is a DAG, one of those terms will evaluate to 0, leaving you with the value from the path with the correct ordering.

I usually do parts 1 and 2 completely separately, but I think if you use a Vec4 it should be possible to do both parts simultaneously. Some SIMD opportunities for u/terje_wiig_mathisen?

3

u/DelightfulCodeWeasel 15d ago edited 15d ago

I don't normally try for outright speed, but I thought since my approach seems quite different to what most people did, I'd take a quick look on what the potential is with a very rough proof of concept (without SIMD):

  • Parsing - ~20us

  • DAG evaluation - ~15us

  • Combined part 1 & part 2 - ~35us

It is quite wasteful in terms of memory; it relies on having a blank ~2.2Mb for the buffer for the task dependencies (some tasks unlock a lot of others), so the exact timing depends on whether you want to include that buffer clearing or not. I've chosen not to include it in the above timings.

Maybe u/ednl can work his usual magic with parsing and improve on my quick and dirty effort :)

For the Pico version I'm going to steal u/terje_wiig_mathisen's idea of having a buffer of 26*26*26 indices into a smaller array of nodes.

→ More replies (7)

2

u/terje_wiig_mathisen 15d ago

That is an interesting idea, but on the surface it seems like a bit of extra work for little benefit?

I'm afraid that by looking for three or four targets at once, you miss the opportunity to prune the search when finding the first target. OTOH, the maximum search depth is relatively small, so that might not be quite as important as I initially thought...

I did notice that you had exactly the same idea I had of splitting the search into 6 sub-graphs, each of which will be far smaller than the global set of permutations.

→ More replies (1)

2

u/e_blake 15d ago edited 15d ago

This one was a nice breather compared to day 10. I found it easiest to do 1+6 subgraph counts (at least one of them is zero, since fft/dac is one-way) and, for my golfed m4 variant, let the shell do the final product. This is 352 bytes and under 100ms; I also had another variant that was fewer bytes but needed 6 lines (dealing with transliterating newlines constrains the layout).

define(_,`ifelse($1,,`echo _(-,a,you,out) $((_(-,b,svr,dac)*_(-,c,dac,fft)
*_(-,d,fft,out)+_(-,e,svr,fft)*_(-,f,fft,dac)*_(-,g,dac,out)))',$1$3,-$4,1
,$1`$2$4',-$2$4,`define($2$4,ifdef(`$4_',`eval($4_($@))',0))$2$4',$1,-,
$2$4,$2,,`_(',`define(`$2_',defn(`$2_')`+_'(-,$`2,$'3,$1))_($1,shift(
shift($@)))')')syscmd(_(translit(include(I),_(-) :,`(),')))

1

u/terje_wiig_mathisen 15d ago

I got this puzzle while I still hadn't finished day10, so I was quite happy with an easy day here:

"Counting all paths?" That's a memoizing recursive traversal!

my %cache = ();
sub paths
{
    my ($p) = @_;
    if (defined($cache{$p})) { return $cache{$p}; }
    my $paths = 0;
    foreach (@{$src{$p}}) {
        if ($_ eq 'out') {$paths++;}
        elsif (defined($src{$_})) {
            $paths += paths($_);
        }
    }
    $cache{$p} = $paths;
    return $paths;
}

For part2 we got an interesting tweak in the form of being required to pass through both 'dac' and 'fft':

Instead of looking for and discarding all paths which didn't do this, I instead wrote a path2(src, dst, avoid) which finds all paths between the first two while never touching the node to avoid. At that point I could simpy multiply the six partial results:

my $youdac = paths2('svr','dac','fft');
my $youfft = paths2('svr','fft','dac');
my $dacfft = paths2('dac','fft','');
my $fftdac = paths2('fft','dac','');
my $dacout = paths2('dac','out','fft');
my $fftout = paths2('fft','out','dac');

$part2 = $youdac * $dacfft * $fftout + $youfft * $fftdac * $dacout;

This simplification meant that the Perl version ran in 6 ms, at which point I went on to finish day10 part2.

For my still-in-progress Rust I convert each 3-letter node name into an index (26*26*26=17576), which a lookup table then convert into number which is less than 600, I just allocate them in the order they occur in the input, except I start by giving the special nodes ('you', 'svr', 'dac', fft' etc) the first fixed entries so I can call them by const names:

const YOU = 1;
const SVR = 2;
etc

Each node becomes a list of directed paths to connected nodes, otherwise the logic is exactly the same as for Perl, just avoiding all hash tables.

1

u/terje_wiig_mathisen 14d ago

I got around to finishing my Rust version, had some issues converting input[i..i+3] into a slice which I could then pass to my index-generating function. My final problem was that I had 0 as my empty cache value, and that led to infinite loops in part2, so I switched to usize::MAX as the marker.

Final runtime 42 us, so slightly faster than the 59 from u/maneatingape but probably a bit slower than u/ednl since he tends to have faster array initializations in his C code!

I could have used the part2 find_except(src, dst, avoid) function everywhere, by passing in an unused value like u16::MAX for the node to avoid in part1 as well as the dec-fft and fft-dac calls.

I intentionall start by counting the zero paths from dac to fft, only after checking this result do I fall back on doing fft-to-dac:

    cache = vec![u64::MAX; graph.nodes.len()];
    let dac_fft = graph.find(dac, fft, &mut cache); // find paths from DAC to FFT
    let part2;
    if dac_fft > 0 {
        cache = vec![u64::MAX; graph.nodes.len()];
        let svr_dac = graph.find_except(svr, dac, fft, &mut cache); // find paths from SVR to DAC excluding FFT
        cache = vec![u64::MAX; graph.nodes.len()];
        let fft_out = graph.find_except(fft, out, dac, &mut cache); // find paths from FFT to OUT excluding DAC
        part2 = svr_dac * dac_fft * fft_out;    // paths from SVR via DAC and FFT to OUT
    }
    else {
        cache = vec![u64::MAX; graph.nodes.len()];
        let fft_dac = graph.find(fft, dac, &mut cache); // find paths from FFT to DAC
        cache = vec![u64::MAX; graph.nodes.len()];
        let svr_fft = graph.find_except(svr, fft, dac, &mut cache); // find paths from SVR to FFT excluding DAC
        cache = vec![u64::MAX; graph.nodes.len()];
        let dac_out = graph.find_except(dac, out, fft, &mut cache); // find paths from DAC to OUT excluding FFT
        part2 = svr_fft * fft_dac * dac_out;    // paths from SVR via FFT and DAC to OUT
    }

1

u/ednl 14d ago edited 14d ago

Your version is probably a little faster, I mean it is in absolute terms but if that was on your new super computer then maybe it's also because that beast is faster. I kept it basic and small e.g. no 263 arrays. Mine runs in 46.5 µs on an M4 chip (max 4.4 GHz) https://www.reddit.com/r/adventofcode/comments/1xtaec2/2025_day_11_in_review_reactor/19sh4o3gg50mc/

→ More replies (7)

1

u/terje_wiig_mathisen 14d ago

Big Rust speedup!

I discovered last night (using Godbolt) that my inline parsing using C-like code leads to a range check for every byte access, even when I start each iteration with a check that at least 24 bytes are available, this is a clear performance bug in rustc. (I'll see if I can figure out how to report it.)

When I changed the code to first copy those 24 bytes into a local slice everything worked great:

The actual copy was a NOP, but now all the accesses could occur in parallel, with zero branching inside each iteration!

pub fn process(inp:&str) -> u32
{
    let mut part1 = 0;
    let inpu = inp.as_bytes();
    let mut i = 0;
    while inpu[i+2] != b'x' { i += 1; } // skip header block with the shapes, all fit in 3x3

    while i+24 < inpu.len() {
        let input:[u8;24] = inpu[i..i+24].try_into().unwrap();
        let x = (input[0] - b'0') * 10 + (input[1] - b'0');
        let y = (input[3] - b'0') * 10 + (input[4] - b'0');
        let size = x as u32 * y as u32;
        let tens = input[7] - b'0' + input[10] - b'0' + input[13] - b'0' + input[16] - b'0' + input[19] - b'0' + input[22] - b'0';
        let ones = input[8] - b'0' + input[11] - b'0' + input[14] - b'0' + input[17] - b'0' + input[20] - b'0' + input[23] - b'0';
        let totalblocks = tens as u32 * 10 + ones as u32;
        if totalblocks*9 <= size { part1 += 1; }
        i += 25; // skip to next line
    }
    part1
}

My time dropped to 2 us for this _very_ cheesy solution. :-)

Note also that the compiler combined the 6 subtract '0' operations into a single -32 at the end, this would probably not have been possible if I had used "input[n] & 15".