Skip to content

Instantly share code, notes, and snippets.

@ken-master
Created February 12, 2016 07:27
Show Gist options
  • Save ken-master/24ec01a33575bc9709fa to your computer and use it in GitHub Desktop.
Save ken-master/24ec01a33575bc9709fa to your computer and use it in GitHub Desktop.
diagonal array summation
<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$a = array();
for($a_i = 0; $a_i < $n; $a_i++) {
$a_temp = fgets($handle);
$a[] = explode(" ",$a_temp);
array_walk($a[$a_i],'intval');
}
$left[0] = array_sum(
array_map(function($row, $index){
return $row[$index];
}, $a, array_keys($a)
)
);
$right[1] = array_sum( array_map(function($row, $index){
return $row[$index];
}, $a, array_reverse(array_keys($a))) );
$result = array_diff($right, $left);
print_r($result);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment