Wednesday, May 29, 2013

The highschool math club problem that bugged me for entirely too long

There is a grid of digits and arithmetic operators, certain paths through the grid result in true mathematical statements. The challenge is to find all of the true statements in the grid.

It's really not that hard of a problem to solve by brute force. I at the time of the competition I tried writing a program to solve it (in the Blitzmax language, because that was the language I knew best at the time). But for whatever reason, I couldn't get it to work quite right.

Fast forward a few years, and it was still bugging me that I never solved that problem, so I wrote up a perl script to do it.

Basically, it's a recursive algorithm that checks every possible path through the puzzle.

The problem can be thought of as an undirected graph. Solutions consist of sets of connected nodes, where each node in the set is passed just once in the traversal. A set of connected nodes is a solution if the values of the nodes, in order, make a valid mathematical statement. Even though the graph is not directional, the solutions are. Because, for example:
15*3 = 45, but 54 != 3*51
(at first I had written: 12*3 = 36, but 63 != 3*21... :-P )
In general, the reverse string of a true mathematical equation is usually not true. So every path, in both directions must be checked.

There are multiple ways to do this, but this algorithm does it by iterating through all of the nodes in the graph (tiles in the puzzle), and finding all possible sets of connected nodes that have that node as the starting point, and which of those sets spell correct equations.

Looking at that perl script now, I see lots of things that make me cringe: for example the regex to parse the input is not quite correct, the data structures I used are probably not the best for this problem (better to preprocess the data into lists probably), and others. Notwithstanding that it does solve the problem it was intended to solve, and since none of the tile-sets given were very large, it solves them instantaneously.