Skip to main content
fix ungolfed version
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
function c( &$m, $x, $r=-1, $d=0 ) {

    // locate starting positionroom ($r) on first move
    if( $r < 0 )                        
        $r = array_search( 'P', $m );

    // mark square on map ($m) with V to show it has been visited
    $m[ $r ] = 'V';

    // end if no more treasures
    if ( ! in_array( 'T', $m ) )
        exit;

    // generate list of adjacent squares
    $dirs = [
        $r % $x ? $r - 1 : -1,            // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x - 1 ? $r + 1 : -1    // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n>=0$n >= 0 and $m[$n]$m[ $n ] != 'M' and $m[$n]$m[ $n ] != 'V' ) {
            // display the direction
            echo 'LUDR'[ $o ];
            // and recursively keep crawling
            c( $m, $x, $n, $r, $o );
        }

    // reached a dead end, go back to previous square
    echo 'RDUL'[ $d ];   // display the reverse direction

}
function c( &$m, $x, $r=-1, $d=0 ) {

    // locate starting position on first move
    if( $r < 0 )                        
        $r = array_search( 'P', $m );

    // mark square with V to show it has been visited
    $m[ $r ] = 'V';

    // end if no more treasures
    if ( ! in_array( 'T', $m ) )
        exit;

    // generate list of adjacent squares
    $dirs = [
        $r % $x ? $r - 1 : -1,            // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x - 1 ? $r + 1 : -1    // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n>=0 and $m[$n] != 'M' and $m[$n] != 'V' ) {
            // display the direction
            echo 'LUDR'[ $o ];
            // and recursively keep crawling
            c( $m, $x, $n, $r, $o );
        }

    // reached a dead end, go back to previous square
    echo 'RDUL'[ $d ];   // display the reverse direction

}
function c( &$m, $x, $r=-1, $d=0 ) {

    // locate starting room ($r) on first move
    if( $r < 0 )                        
        $r = array_search( 'P', $m );

    // mark square on map ($m) with V to show it has been visited
    $m[ $r ] = 'V';

    // end if no more treasures
    if ( ! in_array( 'T', $m ) )
        exit;

    // generate list of adjacent squares
    $dirs = [
        $r % $x ? $r - 1 : -1,            // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x - 1 ? $r + 1 : -1    // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n >= 0 and $m[ $n ] != 'M' and $m[ $n ] != 'V' ) {
            // display the direction
            echo 'LUDR'[ $o ];
            // and recursively keep crawling
            c( $m, $x, $n, $o );
        }

    // reached a dead end, go back to previous square
    echo 'RDUL'[ $d ];   // display the reverse direction

}
-14 bytes, more golfing
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57

PHP, 385 335 307 297 279270 270256 bytes

function c(&$m,$x,$r=-1,$l=-1,$d=0){if($r<0)$r=array_search(P,$m);$m[$l=$r]=V;if;$m[$r]=V;if(!in_array(T,$m))die;foreach([$r%$x?$r-1:-1,$m[$r-$x]?$r-$x:-1,$m[$r+$x]?$r+$x:-1,$r%$x<$x-1?$r+1:-1]as$o=>$n)if($n>=0&&$m[$n]$n>=0&$m[$n]!=M&&$m[$n]=M&$m[$n]!=V){echo LUDR[$o];c($m,$x,$n,$r,$o);}echo RDUL[$d];}

Try it online!Try it online!

Input is a one-dimensional array map and number of columns, example: c( $map, 13 );.
Output is to STDOUT as L,R,U and D (doesn't save enough bytes to make it worth showing as numeric... I prefer the letters).

Or 242 bytes to output as numbers (but I prefer the letters).

This could stand a bit more golfing (and possibly a bit more cleverness).

function c( &$m, $x, $r=-1, $l=-1, $d=0 ) {

    // locate starting position on first move
    if( $r < 0 )                        
        $r = array_search( 'P', $m );

    // move to new square
    $l = $r;

    // mark square with .V to show it has been visited
    $m[ $r ] = '.';'V';

    // end if no more treasures
    if ( ! in_array( 'T', $m ) )
        exit;

    // generate list of adjacent squares
    $dirs = [
        $r % $x ? $r - 1 : -1,            // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x - 1 ? $r + 1 : -1    // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n>=0 &&and $m[$n] != 'M' &&and $m[$n] != '.''V' ) {
            // display the direction
            echo 'LUDR'[ $o ];
            // and recursively keep crawling
            c( $m, $x, $n, $r, $o );
        }

    // reached a dead end, go back to previous square
    echo 'RDUL'[ $d ];   // display the reverse direction

}

Basic CaseBasic Case

PHP, 385 335 307 297 279 270 bytes

function c(&$m,$x,$r=-1,$l=-1,$d=0){if($r<0)$r=array_search(P,$m);$m[$l=$r]=V;if(!in_array(T,$m))die;foreach([$r%$x?$r-1:-1,$m[$r-$x]?$r-$x:-1,$m[$r+$x]?$r+$x:-1,$r%$x<$x-1?$r+1:-1]as$o=>$n)if($n>=0&&$m[$n]!=M&&$m[$n]!=V){echo LUDR[$o];c($m,$x,$n,$r,$o);}echo RDUL[$d];}

Try it online!

Input is a one-dimensional array map and number of columns, example: c( $map, 13 );.
Output is to STDOUT as L,R,U and D (doesn't save enough bytes to make it worth showing as numeric... I prefer the letters).

This could stand a bit more golfing (and possibly a bit more cleverness).

function c( &$m, $x, $r=-1, $l=-1, $d=0 ) {

    // locate starting position on first move
    if( $r < 0 )                        
        $r = array_search( 'P', $m );

    // move to new square
    $l = $r;

    // mark square with . to show it has been visited
    $m[ $r ] = '.';

    // end if no more treasures
    if ( ! in_array( 'T', $m ) )
        exit;

    // generate list of adjacent squares
    $dirs = [
        $r % $x ? $r - 1 : -1,            // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x - 1 ? $r + 1 : -1    // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n>=0 && $m[$n] != 'M' && $m[$n] != '.' ) {
            // display the direction
            echo 'LUDR'[ $o ];
            // and recursively keep crawling
            c( $m, $x, $n, $r, $o );
        }

    // reached a dead end, go back to previous square
    echo 'RDUL'[ $d ];   // display the reverse direction

}

Basic Case

PHP, 385 335 307 270 256 bytes

function c(&$m,$x,$r=-1,$d=0){if($r<0)$r=array_search(P,$m);$m[$r]=V;if(!in_array(T,$m))die;foreach([$r%$x?$r-1:-1,$m[$r-$x]?$r-$x:-1,$m[$r+$x]?$r+$x:-1,$r%$x<$x-1?$r+1:-1]as$o=>$n)if($n>=0&$m[$n]!=M&$m[$n]!=V){echo LUDR[$o];c($m,$x,$n,$o);}echo RDUL[$d];}

Try it online!

Input is a one-dimensional array map and number of columns, example: c( $map, 13 );.
Output is to STDOUT as L,R,U and D.

Or 242 bytes to output as numbers (but I prefer the letters).

function c( &$m, $x, $r=-1, $d=0 ) {

    // locate starting position on first move
    if( $r < 0 )                        
        $r = array_search( 'P', $m );

    // mark square with V to show it has been visited
    $m[ $r ] = 'V';

    // end if no more treasures
    if ( ! in_array( 'T', $m ) )
        exit;

    // generate list of adjacent squares
    $dirs = [
        $r % $x ? $r - 1 : -1,            // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x - 1 ? $r + 1 : -1    // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n>=0 and $m[$n] != 'M' and $m[$n] != 'V' ) {
            // display the direction
            echo 'LUDR'[ $o ];
            // and recursively keep crawling
            c( $m, $x, $n, $r, $o );
        }

    // reached a dead end, go back to previous square
    echo 'RDUL'[ $d ];   // display the reverse direction

}

Basic Case

revert number-based (didn't save as much as thought)
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57

Input is a one-dimensional array map, and number of columns, example: sc( $m$map, 13 );, output list.
Output is to STDOUT as L,R,U and D (doesn't save enough bytes to make it worth showing as numeric... I prefer the letters).

function c( &$m, $x, $r=-1, $l=-1, $d=0 ) {

    // locate starting position on first move
    if( $r < 0 )                        
        $r = array_search( 'P', $m );

    // move to new sqauresquare
    $l = $r;

    // mark square with . to show it has been visited
    $m[ $r ] = '.';

    // end if no more treasures
    if ( ! in_array( 'T', $m ) )
        exit;

    // generate list of adjacent squares
    $dirs = [
        $r % $x ? $r - 1 : -1,            // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x - 1 ? $r + 1 : -1    // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n>=0 && $m[$n] != 'M' && $m[$n] != '.' ) {
            // display the direction
            echo 'LUDR'[ $o ];
            // and recursively keep crawling
            c( $m, $x, $n, $r, $o );
        }

    // reached a dead end, go back to previous square
    echo 'RDUL'[ $d ];   // display the reverse direction

}

Input is a one-dimensional array map, and number of columns s( $m, 13 );, output list to STDOUT.

function c( &$m, $x, $r=-1, $l=-1, $d=0 ) {

    // locate starting position on first move
    if( $r < 0 )                        
        $r = array_search( 'P', $m );

    // move to new sqaure
    $l = $r;

    // mark square with . to show it has been visited
    $m[ $r ] = '.';

    // end if no more treasures
    if ( ! in_array( 'T', $m ) )
        exit;

    // generate list of adjacent squares
    $dirs = [
        $r % $x ? $r - 1 : -1,            // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x - 1 ? $r + 1 : -1    // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n>=0 && $m[$n] != 'M' && $m[$n] != '.' ) {
            // display the direction
            echo 'LUDR'[ $o ];
            // and recursively keep crawling
            c( $m, $x, $n, $r, $o );
        }

    // reached a dead end, go back to previous square
    echo 'RDUL'[ $d ];   // display the reverse direction

}

Input is a one-dimensional array map and number of columns, example: c( $map, 13 );.
Output is to STDOUT as L,R,U and D (doesn't save enough bytes to make it worth showing as numeric... I prefer the letters).

function c( &$m, $x, $r=-1, $l=-1, $d=0 ) {

    // locate starting position on first move
    if( $r < 0 )                        
        $r = array_search( 'P', $m );

    // move to new square
    $l = $r;

    // mark square with . to show it has been visited
    $m[ $r ] = '.';

    // end if no more treasures
    if ( ! in_array( 'T', $m ) )
        exit;

    // generate list of adjacent squares
    $dirs = [
        $r % $x ? $r - 1 : -1,            // Left
        $m[ $r - $x ] ? $r - $x : -1,     // Up
        $m[ $r + $x ] ? $r + $x : -1,     // Down
        $r % $x < $x - 1 ? $r + 1 : -1    // Right
    ];

    // consider valid directions for next move
    foreach ( $dirs as $o => $n )
        // if not a Wall or Mountain and not Visited
        if ( $n>=0 && $m[$n] != 'M' && $m[$n] != '.' ) {
            // display the direction
            echo 'LUDR'[ $o ];
            // and recursively keep crawling
            c( $m, $x, $n, $r, $o );
        }

    // reached a dead end, go back to previous square
    echo 'RDUL'[ $d ];   // display the reverse direction

}
Rollback to Revision 15
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
-14 bytes, use numbers instead of letters for output
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
-9 bytes for unnecessary condition, clean up all test cases
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
deleted 1 character in body
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
-18 bytes, use string instead of array for direction table
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
fix all test cases
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
comment fix
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
bugfix for treasure in square 0, +11 bytes
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
-3 bytes
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
-3 bytes
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
added 131 characters in body
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
deleted 6 characters in body
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
deleted 6 characters in body
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
deleted 19 characters in body
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
ungolfed version
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
deleted 674 characters in body
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading
Source Link
640KB
  • 11.9k
  • 2
  • 35
  • 57
Loading