Another Advent of Code classic!

The Preliminary

I just had Gemini fill this part out: these are the most tedious bits and it’s nice to just scope out: “hey I want classes with this API: go implement!”. It’s moments like this where it does feel like moving up an abstraction layer. Nothing particularly interesting here.

abstract type Button end

struct AppendButton <: Button
    val::Int
end

struct MultButton <: Button
    val::Int
end

struct DivButton <: Button
    val::Int
end

struct ReplaceButton <: Button
    origin::String
    target::String
end
ReplaceButton(origin::Int, target::Int) = ReplaceButton(string(origin), string(target))

struct OctButton <: Button end

struct BackSpaceButton <: Button end

function op(state::Int, button::AppendButton)
    return state == 0 ? button.val : state * 10^ndigits(button.val) + button.val
end

op(state::Int, button::MultButton) = state * button.val
op(state::Int, button::DivButton) = div(state, button.val)
op(state::Int, button::ReplaceButton) =
    parse(Int, replace(string(state), button.origin => button.target))
op(state::Int, ::OctButton) = parse(Int, string(state; base = 8))
op(state::Int, ::BackSpaceButton) = div(state, 10)

function parse_button(token::AbstractString)::Button
    body = strip(token, ['[', ']'])
    if body == "oct"
        return OctButton()
    elseif body == "bksp"
        return BackSpaceButton()
    elseif startswith(body, "*")
        return MultButton(parse(Int, body[2:end]))
    elseif startswith(body, "/")
        return DivButton(parse(Int, body[2:end]))
    elseif occursin("->", body)
        origin, target = split(body, "->")
        return ReplaceButton(String(origin), String(target))
    else
        return AppendButton(parse(Int, body))
    end
end

parse_buttons(line) = Button[parse_button(tok) for tok in split(line)]

Part 1

That said nothing particularly interesting here either: just banging out an exhaustive search with some redundancy avoidance. You could also do a breadth-first search which would let you dedupe states locally but the DFS sets us up for a wider array of potential part 2s.

function reachable_by_depth(buttons, max_depth = 3)
    seen = Set{Tuple{Int,Int}}()
    reachable = Set{Int}()
    explore = [(0, 0),]

    while !isempty(explore)
        depth, state = pop!(explore)
        (depth, state) in seen && continue
        push!(reachable, state)
        depth == max_depth && continue
        push!(seen, (depth, state))

        for button in buttons
            push!(explore, (depth + 1, op(state, button)))
        end
    end

    return sum(reachable)
end


function part1(fname = "input.txt")
    buttons = parse_buttons(readline(fname))
    return reachable_by_depth(buttons)
end

Part 2

But as it turns out this part is actually even easier since we don’t have to care about the depth state anymore. I suspect the intent was to catch folks who didn’t add a seen check.

function all_discoverable(buttons, max_width = 4)
    seen = Set()
    reachable = Set()
    explore = [0]

    while !isempty(explore)
        state = pop!(explore)
        state > 10 ^ max_width - 1 && continue
        state in seen && continue
        push!(seen, state)
        push!(reachable, state)

        for button in buttons
            push!(explore, op(state, button))
        end
    end

    return sum(reachable)
end

function part2(fname = "input.txt")
    buttons = parse_buttons(readline(fname))
    return all_discoverable(buttons)
end

Something Extra

I was curious about what the graph structure looked like:

Fortunately you can see that our “seen” cache actually does help us prevent some expansions. I found it interesting just how many no-ops there were: none of the nodes have a full 9 potential expansions.

It gets better with part 2. I was sort of expecting to be able to reach most numbers.

And indeed we get to a lot.

For instance we get to every number below 1,000. That immediately gets us anything ending with a 7 or 9. We also do quite well under 3333 (due to a [/3] instruction). But then it starts getting sparse. There’s a big gap at 7777 where the [oct] instruction peters out. But then the 9000s are well-represented again due to the [1->9] instruction (except for numbers with other 1 digits). My favorite pattern is the diagonal [*9] bands.