Started at 22:11 03.09.2020 Thursday Sep.
Finished at 22:44 03.09.2020 Thursday Sep. (0hrs 33minutes)
Last active
September 3, 2020 15:45
-
-
Save lbvf50mobile/3de3307b424a1f2e01138526b13d02ff to your computer and use it in GitHub Desktop.
Just PHP FUN 093.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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