Tagline written on the box: "Over 300,000 wrong ways to assemble the pieces, but only One right way! This is truly... One Tough Puzzle!"
I found this jigsaw puzzle in my mom's house a couple of weeks ago with the daunting title "One Tough Puzzle". The back of the box reads that it was developed by the Great American Puzzle Factory and upon looking them up, I found that the puzzle's production has been discontinued, but that's neither here nor there.
The puzzle's schtick lies in its surface-level simplicity- the whole thing is made of only nine square, non-descript red pieces, and each piece's interlocking sides are a random mix of the four main suits (e.g club, diamond, heart, spade), where two of its joints jut out and two cut in. Two pieces can then be connected at a point if, for example, one has a side with an outward-facing heart and the other has an inward-facing heart. So the idea then is to connect every piece into a single 3x3 grid. If it's been constructed correctly, the box reads that the grid's sides should have exactly six faces pointing out and six faces pointing in. No other instructions are provided.
The tag line on the box, quoted at the top of the ReadMe, gets the gist of it. The problem statement is simple, but there end up being so many possible combinations that finding the correct solution ends up being like pulling a needle out of a haystack. Playing around with it by hand, it was relatively easy to get 8 of the 9 pieces together, but the last one never really fit, hence the difficulty.
So this project is a relatively simple algorithm I wrote to solve it, optimizing some general ideas here and there in order to get to a solution more quickly, trying more likely solutions first and avoiding repeated work where possible. Not that this was the goal, but this program does technically extend to any NxN puzzle with the same rule set, since the solution to one size can be pretty naturally extended to any size at all. According to my napkin math, anything over a 5x5 solution would be impossible for a human to ever do, since that would be getting into the quadrillions of possibilites. So if you ever need a solution to something like that, try this algorithm out. You're welcome.
-
Clone the project from Git, or download it into any empty directory.
-
The program can be compiled by typing
makein the command line from the parent directory. -
To give the program its "pieces" to work with, create a file in the directory called
pieces.txt.a. Starting on the first line, write the faces on each piece into each new line, with a single space between each face. To write a "face", use this map:
- spade = 0
- club = 1
- diamond = 2
- heart = 3
b. Face ordering also matters, since the sides are relative to one another, and some face out while others face in. For this program to work, orient each piece such that one of its outward facing sides points up and the other one right. Now, consider the top-facing side as the first face (index 0) and index clockwise.
c. This should leave you with a pieces.txt file that looks something like this:
0 0 3 1 2 1 1 2 3 2 1 1 3 2 2 3 1 3 0 3 1 3 2 1 0 2 0 3 3 0 0 1 0 2 3 2
-
Run the algorithm by typing
./otp_solverin the command line. This will output asolution.txtfile into your folder that contains the answer. It should look something like this:Example output:
Note: Parenthesized numbers describe the right-pointing face index. This is the solution: 8(1) 5(0) 2(0) 7(1) 4(0) 1(0) 6(1) 3(0) 0(0)
-
To clear the compiled code and
.ofiles, typemake clean.