To make it faster requires to optimize the forEach loop mostly. In this loop, we are looking for the point that has :
- exactly 1 neighbor
- isn't plot already
- has minimum weight
The problem is that every time the loop is done, neighbor count is recalculated for every (X,Y). I tried to add a 3rd Z layer on the Array, storing the number of neighbor for every point, and maintaining it every time we plot one more time (so only in the plot function). This let me remove completely the greedy calculation of the local "neighbors", that was removed too.
Last optimization, changing the organisation of the ForEach Loop to check the conditions in a special order :
- first, neighbor count, as the set of cells with only 1 neighbor is finite and minimal in the maze
- second, applying the "not constructed" condition"
- last, the most greedy, the "minimum weight" check
It works consistantly on a bigger maze, I didn't try to go higher than 100x100 though