View Single Post
Old 11-23-2005, 03:31 AM   #1 (permalink)
KnifeMissile
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
How to solve ShapeShifter...

I'm dating a girl who likes to play on Neopets (what can I say, I like'em young!), where you can get neopoints (in-game currency) by succeeding in various games. One such game is Shapeshifter. It looks like this:



The object of the game is to place the various shapes, in order, on the board, so that all the symbols are the goal symbol, as indicated in the diagram with the big red arrows. Each symbol that's under a shape placed on the board gets turned into the next symbol according to, again, the big red arrow diagram.
I think it's fairly self explanatory.

My question is how can one solve this puzzle, efficiently?

It started out as a simple software project that I did for the fun of it, since the puzzle seemed so interesting to me and it's rated as "extremely difficult" by Neopets. My first implementation simply recursed through all possible board positions to find a solution. This worked for a while but, as you solved one puzzle after the other, they gradually become much harder and, subsequently, the program had to run much longer. Eventually, I added a progress bar and a prediction as to when the program will solve the puzzle, since no one likes to wait around without knowing how much longer you'll things will take. In actuality, the program can't really know how long it will take but it could calculate an upper bound for the time needed and that's what it reported.
Much to my chagrin, it didn't take long before the upper bound time to solution was measured in years! So, not one to easily give up, I was forced to actually think about the problem. What will follow is a description of the evolution of my current implementation for solving Shapeshifter. If you'd rather not be told of any of the properties of the puzzle so that you can solve it "entirely by yourself," then don't look into the spoiler tags.
Spoiler: I brought it up with some friends and one of them, Yakk actually (and why to links penetrate the spoiler tag?), pointed out that the corners could only be reached in one way, and sometimes not even at all! While it was not immediately obvious how this fact can be used to my advantage, it was clearly a restriction of the solution space and, thus, (theoretically) something that can shorten my search time.

After mulling over the problem for a while, other properties of the puzzle occured to me. The order that the shapes are applied is irrelevant. The special properties of the corners can be generalized to all the points on the board. Not every shape can reach every piece. Each board point has certain shapes that can reach it and in a varying number of ways. Finally, each piece needs a specific number number of pieces to solve it. In the case of two symbols (such as the puzzle image used here) a board point that's already the target symbol needs an even number of shapes on it, while another point that's not the target symbol needs an odd number of them.

After these considerations, I concieved of a different algorithm. Inspect the puzzle one board point at a time. Sort them in order of available shape positions on them. For each board point to inspect, iterate over all possible combinations of necessary numbers of shapes that will solve that board point and recurse. Surprisingly, this drastically reduces the number of board positions being evaluated and cuts tree searches off earlier.
This reduces time to solutions to within a day, rather than years! Now that's an optimisation!

Still, a day is a rather long time to wait for a solution. Can't we do better? With a few refinements and some micro-optimisations, I've been able to reduce time to solution to (experimentally, since execution times with algorithms this complex are too hard to calculate accurately) within two or so hours. Not bad but still... Can we still do better?

So, here I am. I'm almost at my end. There's one last property of the puzzle that I was able discern recently and, thus, one last optimisation I can make but it won't recduce hours to seconds. Spoiler: If you look at the sheer number of shapes (it gets worse later on), you can see that there are a fair number of shapes on each square. Counting shape squares and dividing by the number of board points, you will see that the number of shapes over each square can average between 2.4 to 3.7 shapes! Seems a little weird to start guessing number of shapes over a (two symbol) square at zero or one, right? Now, the situation is a little more complicated than that because, since the corners and edges are less accessible, the average number of shapes over a board point varies even within the same puzzle board! Luckily, this just makes the situation more advantagous, not less. The corners average about none, while the center squares can have as many as eight shapes over them! That makes it totally ridiculous to start guessing at zero... while at the corners, you'd want to guess the exact number of shapes needed.

But that's it. That's where I am and I'm all out of ideas. If anyone else can think of more that can be done, or if there's a totally foreign appraoch that might do better, please mention it!
Thank you...
KnifeMissile is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360