## more, please!

As The Riddler proposed for several weeks in a row a CrossProduct™ puzzle where 3 x n one-digit integers have to be deduced from their rowwise and columnwise products, I attempted writing an R solver by applying a few basic rules repeatedly, which worked for the first two puzzles, if not for the earlier one I solved by paper & pen (mostly) a few weeks ago, and again worked for the final one. The rules I used were to spot unique entries, forced entries by saturation, and forced digits (from the prime factor decomposition) again by saturation. Any further rule to add to the solver? (The R code is currently rather ugly!) Please, some more!

February 27, 2021 at 12:59 am

I love these kinds of constraint satisfaction puzzles. I used to work on constraint satisfaction in the early 1990s. It also reminds me of the top tier Top Coder problems I used to do when I moved from academia to industry and needed to get a lot better at coding in a language like C++.

I’m appending some C++ code to solve your problem. It takes 0.06s to solve this one (O3 optimization with clang++ on an iMac Pro):

7, 7, 6 [294]

9, 8, 3 [216]

9, 5, 3 [135]

7, 2, 7 [98]

8, 2, 7 [112]

7, 4, 3 [84]

5, 7, 7 [245]

8, 5, 1 [40]

(8890560), (156800), (55566)

The algorithm is a simple backtracking algorithm, like one might learn in an intro algorithms class as a solution to the 8-queens puzzle. All it does is try to fill in from upper left one square at a time (by row), only extending with guesses by which the row and column products are divisible and for which completed rows have the right product. There would be a lot of ways to make this go much faster. For example, the last entry in a row is deterministic given the row products. Also, only the last entry added needs to be checked for consistency given the way the extension and backtracking work.

This is the same basic constraint resolution algorithm I used to build a Sudoku solver in Java after trying my hand at one on a flight.

As usual, I thought I could code this in 30m, but my index fiddling just isn’t what it was back in my Top Coder practice days, so I wound up whiling away 2 hours of my Friday afternoon on it.

And here’s what the run looks like:

February 27, 2021 at 1:02 am

After the program was primed, subsequent runs took 0.06s! Also, the editor ate all of my angle brackets. I forgot how unfriendly WordPress is to pre blocks. All the vectors are vector<int>, and includes are exception, iostream, and vector. Luckily, it didn’t eat the comparison < symbols.

February 27, 2021 at 7:20 pm

Thanks for the code and the two hours, Bob!

February 26, 2021 at 8:11 am

[…] article was first published on R – Xi'an's Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) […]