Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active August 7, 2020 18:31
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/3ed1331be1be83e2ab54cc7bf886ff69 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/3ed1331be1be83e2ab54cc7bf886ff69 to your computer and use it in GitHub Desktop.
Just PHP FUN 070.
<?php
# https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1 Snail.
function snail(array $array): array {
$size = count($array) * count($array[0]);
$width = count($array[0]); $ans = [];
$steps = []; $steps[] = $width; $width -= 1;
for( ; $width > 0; $width -= 1){ $steps[] = $width; $steps[] = $width;}
$i = 0; $j = -1;
// var_dump($steps);
for($x = 0; !empty($steps); $x += 1){
$number = array_shift($steps); $dx = 0; $dy = 0;
if(0 == $x % 4) {$dx = 0; $dy = 1;}
else if (1 == $x % 4) {$dx = 1; $dy = 0;}
else if (2 == $x % 4) {$dx = 0; $dy = -1;}
else { $dx = -1; $dy = 0; }
for($stp = 0; $stp < $number; $stp += 1){
$i += $dx; $j += $dy;
//echo "$i:$j:".$array[$i][$j]." ";
array_push($ans,$array[$i][$j]);
}
}
//echo "\n";
return $ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment