Started at 23:01 07.08.2020 Friday Aug.
Finished at 1:30 08.08.2020 Saturday Aug. ( 2hrs 29minutes)
Last active
August 7, 2020 18:31
-
-
Save lbvf50mobile/3ed1331be1be83e2ab54cc7bf886ff69 to your computer and use it in GitHub Desktop.
Just PHP FUN 070.
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/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