This is a nice solution.
I too was thinking that perl might not be the best tool for solving this, but for an entirely different reason.
I wasn't thinking of this puzzle as a constraint satisfaction problem. Of course not, since it's
linear,
which is what my second solution uses.
But once you mentioned it, viewing as a constraint satisfaction problem also makes sense. After all,
like I noticed for my first solution, once you decide about the buttons of the first row, they determine all the other buttons easily. So much that if you write the program as a constraint program in the natural way, the constraint solver can also determine all the buttons from the ones in the first row, thus a constraint solution must be at least as fast as my first solution.
Update: for reference, the last time salva has surprised me with a nice solution using a finite domain constraint solver was Re^4: Seven by seven farming puzzle.
Update: I ran the solution for (20, 20, 2). Your prolog solution took two and a half minutes (I have modified the printing part somewhat, but used GNU prolog). My perl solution took 40 seconds. So my opinion is that this prolog+finite-domain solution is fast enough. (Update: the same solution ran in SWI prolog is riddiculously slow though.)
| [reply] |