Skip to content

Instantly share code, notes, and snippets.

@gpfiel
Created April 20, 2016 21:05
Show Gist options
  • Save gpfiel/478ba0d72984ef3527d964b799bec539 to your computer and use it in GitHub Desktop.
Save gpfiel/478ba0d72984ef3527d964b799bec539 to your computer and use it in GitHub Desktop.
Question3Toptal
<?php
function solution($A)
{
$sum = 0;
while (count($A)) {
// remove the last part
array_pop($A);
// remove and sum the first line
$sum += array_sum((array)array_shift($A));
// remove and sum the last column
array_walk($A, function(&$line) use (&$sum) {
$sum += array_pop($line);
});
// remove the first column
array_walk($A, function(&$line) use (&$sum) {
array_shift($line);
});
$sum += array_sum((array)array_pop($A));
// remove first line
array_shift($A);
// remove the last column
array_walk($A, function(&$line) use (&$sum) {
array_pop($line);
});
// remove and sum the first column
array_walk($A, function(&$line) use (&$sum) {
$sum += array_shift($line);
});
}
return $sum;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment