010203040506

Advent of Code

2026/5

Rhythmic Factory

in C#

by encse

You exit the mountain pass and turn the corner to arrive at some kind of large manufacturing facility. Although the facility produces a vast assortment of useful components, what you're here for today is multiple large tanks of magic smoke.

The Elves don't need any help loading the magic smoke onto the train, so you spend a few minutes exploring the interesting machines in the facility. In one room, a group of Elves is struggling to operate what appears to be a normal microwave.

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

namespace AdventOfCode.Y2026.Day05;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

using Combinations = System.Collections.Generic.HashSet<(long num, int? depth)>;

[ProblemName("Rhythmic Factory")]
class Solution : Solver {

    public object PartOne(string input) => Solve(input, 3);

    public object PartTwo(string input) => Solve(input, null);

    long Solve(string input, int? depth) {
        var buttons = Parse(input);
        var combinations = new Combinations();
        Collect(buttons, 0, combinations, depth);
        return combinations
            .Select(x => x.num)
            .Distinct()
            .Sum();
    }

    void Collect(List<Button> buttons, long n, Combinations c, int? remainingPresses) {
        if (!c.Add((n, remainingPresses))) {
            return;
        }

        if (remainingPresses == 0) {
            return;
        }

        foreach (var button in buttons) {
            var next = button(n);

            if (next > 9999) {
                continue;
            }

            // this also works for depth == null, which becomes null
            Collect(buttons, next, c, remainingPresses - 1);
        }
    }

    delegate long Button(long n);
    Button Mul(long arg) => num => num * arg;
    Button Div(long arg) => num => num / arg;
    Button Replace(string a, string b) => num => long.Parse(num.ToString().Replace(a, b));
    Button Oct() => num => long.Parse(Convert.ToString(num, 8));
    Button Const(long arg) => num => 10 * num + arg;

    List<Button> Parse(string input) {
        var res = new List<Button>();
        var parts = input.Split(' ').Select(p => p.Trim("[]".ToCharArray()));

        // 🎄 ehh, why not
        foreach (var part in parts) {
            int.TryParse(Regex.Match(part, @"\d+").Value, out var arg);

            Button button = part switch {
                "oct" => Oct(),
                "bksp" => Div(10),
                _ when part.StartsWith("*") => Mul(arg),
                _ when part.StartsWith("/") => Div(arg),
                _ when part.Split("->") is [var a, var b] => Replace(a, b),
                _ => Const(arg),
            };
            res.Add(button);
        }

        return res;
    }
}

Please ☆ my repo if you like it!

© 2026 | Advent of Code is a registered trademark in the US | Images provided by ChatGPT (2026+), in previous years Nano Banana and Bing