Match Cord - solution is very slow - speed up suggestion appreciated


#1

The algorithm works
but in most cases
execution takes too long
for success

https://codecombat.com/play/level/match-cord?session=589cb85abbf8292000e55dee&observing=true

Any advice on optimizing code appreciated

Check around on mines above, below, right, left
relative current mine
if mined sends the mine row and column in array nearestMines
after it
as a new point to check around
mine with index 0 from nearestMines set
previous mine removed from map
by replacing on "."
if the algorithm went into - no mines around
then - reset of map
deadlock point is removed
all over again

// Ogres mined the field to protect their Chieftain.
// But we can use the "domino" effect get our target.
// The scout has prepared the map of the minefield.
// All mines are placed the same distance apart.
// The map is an array of strings, where "x" is a mine and "." is nothing.
// The first row in the array is the row nearest to the hero.

// The map and helpful constants are listed below.

var fieldMap = hero.findFriends()[0].getMap();

var reset = function()
{
    map = [];
    //map = fieldMap.slice();
    for ( var fieldMap_r = 0; fieldMap_r < fieldMap.length; fieldMap_r++ )
    {
        map.push( [] );
        for ( var fieldMap_c = 0; fieldMap_c < fieldMap[ fieldMap_r ].length; fieldMap_c++ )
        {
            var temp = fieldMap[ fieldMap_r][ fieldMap_c ];
            map[ map.length - 1 ].push( temp );
        }
    }
};



var mine = "x";
var empty = ".";
var mineDistance = 5;
var firstXPos = 15;
var firstYPos = 40;


var toXY = function( row, column )
{
    var y = row * mineDistance + firstYPos;
    var x = column * mineDistance + firstXPos;
    hero.say( x + " " + y );
};

var findNearestMines = function( row, column )
{
    if ( row + 1 < map.length
        && column < map[ row + 1 ].length )
    {
	    var top = map[ row + 1][ column ];
    }
	if ( top == mine )
	{
	    //toXY( row + 1, column );
	    nearestMines.push( { r : row + 1, c : column } );
	}

    if ( row - 1  >= 0
        && column < map[ row ].length )
    {
	    var bottom = map[ row - 1][ column ];
    }
	if ( bottom == mine )
	{
	    //toXY( row - 1, column );
		nearestMines.push( { r : row - 1, c : column } );
	}

    if ( row < map.length
        && column - 1 >= 0 )
    {
	    var left = map[ row ][ column - 1 ];
    }
	if ( left == mine )
	{
	    //toXY( row, column - 1 );
        nearestMines.push( { r : row, c : column - 1 } );
	}

	if ( row < map.length
        && column + 1 < map[ row ].length )
    {
	    var right = map[ row ][ column + 1 ];
    }
	if ( right == mine )
	{
	    //toXY( row, column + 1 );
		nearestMines.push( { r : row, c : column + 1 } );
	}
}

// Find which starting mine connects to the ogre Chieftain.
//hero.say( fieldMap[1][26] );//26
//hero.say( fieldMap[15][0] );//15
//15, 13 chieftan

var row = 15;
var column = 13

var map = [];
var excluded = [];

reset();
while ( true )
{
    if ( row < 1 )
    { 
        hero.moveXY( column * mineDistance + firstXPos, firstYPos );
    }

    var nearestMines = [];
    var usedMines = [];
    
    findNearestMines( row, column );

    if ( nearestMines.length < 1 )
    {
        //toXY( row, column );
        excluded.push( { r : row, c : column } );
        reset()
        for ( var excluded_i = 0; excluded_i < excluded.length; excluded_i++ )
        {
            var exclude = excluded[ excluded_i ];   
            map[ exclude.r ].splice( exclude.c, 1, "." );
        }
        row = 15;
        column = 13
        continue;
    }

    map[ row ].splice( column, 1, "." )
    
    //toXY( row, column );
    
    if ( nearestMines.length > 0 )
    {
        row = nearestMines[ 0 ].r;
        column = nearestMines[ 0 ].c;
    }
    
}