Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active September 3, 2020 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/3de3307b424a1f2e01138526b13d02ff to your computer and use it in GitHub Desktop.
Save lbvf50mobile/3de3307b424a1f2e01138526b13d02ff to your computer and use it in GitHub Desktop.
Just PHP FUN 093.
<?php
# https://www.codewars.com/kata/58663693b359c4a6560001d6 Maze Runner.
function maze_runner($maze, $directions): string {
$size = count($maze);
$out = function($point) use ($size){
if(0 > $point[0] || 0 > $point[1]) return true;
if($size <= $point[0] || $size <= $point[1]) return true;
return false;
};
$pos = false;
for($i = 0; $i < $size && ! $pos; $i+=1) for($j = 0; $j < $size && ! $pos; $j+=1)
if(2 == $maze[$i][$j]) $pos = [$i,$j];
$act = [
"N" => function() use (&$pos){ $pos[0] -= 1;},
"E" => function() use (&$pos){ $pos[1] += 1;},
"W" => function() use (&$pos){ $pos[1] -= 1;},
"S" => function() use (&$pos){ $pos[0] += 1;},
];
foreach($directions as $step){
list($i0,$j0) = $pos;
$old = $maze[$i0][$j0];
$act[$step]();
list($i,$j) = $pos;
$curr = $maze[$i][$j];
// echo "[$i0,$j0]=>$old $step [$i,$j]=>$curr \n";
if(3 == $maze[$i][$j]) return "Finish";
if(1 == $maze[$i][$j]) return "Dead";
if($out($pos)) return "Dead";
}
return "Lost";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment