Nice. It reminds me of how compilers generate the optimal sequence of ADD+MUL for a division by a constant; in that case, the algorithm is simpler and has been synthesized in closed form, so the compiler can simply implement the logic (it doesn't need to "bruteforce" its way to it).
This is for instance Go's implementation which is readable and well documented:
https://github.com/golang/go/blob/master/src/cmd/compile/int...