Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $input = <<<END
- 5 6
- 1 2 3 2 1 0
- 2 3 4 6 10 4
- 3 5 7 8 9 6
- 2 3 4 7 8 7
- 1 2 3 6 2 1
- END;
- $input = trim($input);
- $input = explode("\n", $input);
- foreach($input as $key => $value){
- $input[$key] = trim($value);
- }
- foreach($input as $key => $value){
- if($key == 0){
- $table_info = explode(" ", $value);
- }else{
- $table[$key-1] = explode(" ", $value);
- }
- }
- $greatest = array(0, 0, 0);
- for($y = 0; $y < $table_info[0]; $y++){
- $incrementing = 1;
- $last = null;
- $has_incremented = 0;
- for($x = 0; $x < $table_info[1]; $x++){
- if($incrementing == 1 && $last < $table[$y][$x] && $x != ($table_info[1]-1)){
- $last = $table[$y][$x];
- $has_incremented = 1;
- if($last > $greatest[0]){
- $greatest = array($last, $x, $y);
- }
- }elseif($has_incremented == 0){
- $out = $y+1;
- die("row $out is not a hill vector");
- }elseif($last == $table[$y][$x]){
- $out = $y+1;
- die("row $out is not a hill vector");
- }else{
- $incrementing = 0;
- }
- if($incrementing == 0 && $last > $table[$y][$x]){
- $last = $table[$y][$x];
- }elseif($incrementing == 0){
- $out = $y+1;
- die("row $y is not a hill vector");
- }
- }
- }
- for($x = 0; $x < $table_info[1]; $x++){
- $incrementing = 1;
- $last = null;
- $has_incremented = 0;
- for($y = 0; $y < $table_info[0]; $y++){
- if($incrementing == 1 && $last < $table[$y][$x] && $y != ($table_info[0])){
- $last = $table[$y][$x];
- $has_incremented = 1;
- if($last > $greatest[0]){
- $greatest = array($last, $x, $y);
- }
- }elseif($has_incremented == 0){
- $out = $x+1;
- die("column $out is not a hill vector");
- }elseif($last == $table[$y][$x]){
- $out = $x+1;
- die("column $out is not a hill vector");
- }else{
- $incrementing = 0;
- }
- if($incrementing == 0 && $last > $table[$y][$x]){
- $last = $table[$y][$x];
- }elseif($incrementing == 0){
- $out = $x+1;
- die("column $out is not a hill vector");
- }
- }
- }
- $column = $greatest[1]+1;
- $row = $greatest[2]+1;
- echo "peak found at row $row column $column";
Advertisement
Add Comment
Please, Sign In to add comment