Thanks. I asked this to help you make your question clearer if you resubmit it.
It’s an interesting question for HN, so you should try resubmitting it
Perhaps there are some useful ideas in this code: https://www.chiark.greenend.org.uk/~sgtatham/puzzles