Guest User

Untitled

a guest
Dec 11th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. <?php
  2. $rustart = getrusage();
  3.  
  4. $grid_size = 300;
  5. $serial = 5468;
  6. $points = array();
  7.  
  8. for ($y = 0; $y < $grid_size; $y++) {
  9.     for ($x = 0; $x < $grid_size; $x++) {
  10.  
  11.         $points[$y][$x] = (((((($x + 10) * $y + $serial) * ($x + 10)) / 100) >> 0) % 10) - 5;
  12.  
  13.         $dx1 = 0;
  14.         if ($x > 0) {
  15.             $dx1 = $points[$y][$x - 1];
  16.         }
  17.         $dy1 = 0;
  18.         $dx2 = 0;
  19.         if ($y > 0) {
  20.             $dy1 = $points[$y - 1][$x];
  21.             if ($x > 0) {
  22.                 $dx2 = $points[$y - 1][$x - 1];
  23.             }
  24.         }
  25.  
  26.         $points[$y][$x] = $points[$y][$x] + $dy1 + $dx1 - $dx2;
  27.     }
  28. }
  29.  
  30. function find_largest($points, $start, $end)
  31. {
  32.     $grid_size = count($points);
  33.     $fx = 0;
  34.     $fy = 0;
  35.     $largest_sum = 0;
  36.     $fsize = 0;
  37.  
  38.     for ($find_size=$start;$find_size<=$end;$find_size++)
  39.         for ($y = 1; $y < $grid_size-$find_size; $y++) {
  40.             for ($x = 1; $x < $grid_size-$find_size; $x++) {
  41.  
  42.                 $p1 = $points[$y-1][$x-1];
  43.                 $p2 = $points[$y-1][$x-1+$find_size];
  44.                 $p3 = $points[$y-1+$find_size][$x-1+$find_size];
  45.                 $p4 = $points[$y-1+$find_size][$x-1];
  46.  
  47.                 $sum = $p3 - $p2 - $p4 + $p1;
  48.  
  49.                 if ($sum > $largest_sum) {
  50.                     $largest_sum = $sum;
  51.                     $fx = $x;
  52.                     $fy = $y;
  53.                     $fsize = $find_size;
  54.                 }
  55.             }
  56.         }
  57.  
  58.     return array('x'=>$fx, 'y'=>$fy, 'size'=>$fsize);
  59. }
  60.  
  61. echo vsprintf('Part 1: %s,%s', find_largest($points, 3, 3));
  62. echo PHP_EOL;
  63. echo vsprintf('Part 2: %s,%s,%s', find_largest($points, 4, ceil(sqrt($grid_size))));
  64. echo PHP_EOL;
  65.  
  66. function rutime($ru, $rus, $index) {
  67.     return ($ru["ru_$index.tv_sec"]*1000 + (int)($ru["ru_$index.tv_usec"]/1000))
  68.         -  ($rus["ru_$index.tv_sec"]*1000 + (int)($rus["ru_$index.tv_usec"]/1000));
  69. }
  70.  
  71. $ru = getrusage();
  72. echo "This process used " . rutime($ru, $rustart, "utime") .
  73.     " ms for its computations\n";
  74. echo "It spent " . rutime($ru, $rustart, "stime") .
  75.     " ms in system calls\n";
Advertisement
Add Comment
Please, Sign In to add comment