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