Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $rustart = getrusage();
- $grid_size = 300;
- $serial = 5468;
- $points = array();
- for ($y = 0; $y < $grid_size; $y++) {
- for ($x = 0; $x < $grid_size; $x++) {
- $points[$y][$x] = (((((($x + 10) * $y + $serial) * ($x + 10)) / 100) >> 0) % 10) - 5;
- $dx1 = 0;
- if ($x > 0) {
- $dx1 = $points[$y][$x - 1];
- }
- $dy1 = 0;
- $dx2 = 0;
- if ($y > 0) {
- $dy1 = $points[$y - 1][$x];
- if ($x > 0) {
- $dx2 = $points[$y - 1][$x - 1];
- }
- }
- $points[$y][$x] = $points[$y][$x] + $dy1 + $dx1 - $dx2;
- }
- }
- function find_largest($points, $start, $end)
- {
- $grid_size = count($points);
- $fx = 0;
- $fy = 0;
- $largest_sum = 0;
- $fsize = 0;
- for ($find_size=$start;$find_size<=$end;$find_size++)
- for ($y = 1; $y < $grid_size-$find_size; $y++) {
- for ($x = 1; $x < $grid_size-$find_size; $x++) {
- $p1 = $points[$y-1][$x-1];
- $p2 = $points[$y-1][$x-1+$find_size];
- $p3 = $points[$y-1+$find_size][$x-1+$find_size];
- $p4 = $points[$y-1+$find_size][$x-1];
- $sum = $p3 - $p2 - $p4 + $p1;
- if ($sum > $largest_sum) {
- $largest_sum = $sum;
- $fx = $x;
- $fy = $y;
- $fsize = $find_size;
- }
- }
- }
- return array('x'=>$fx, 'y'=>$fy, 'size'=>$fsize);
- }
- echo vsprintf('Part 1: %s,%s', find_largest($points, 3, 3));
- echo PHP_EOL;
- echo vsprintf('Part 2: %s,%s,%s', find_largest($points, 4, ceil(sqrt($grid_size))));
- echo PHP_EOL;
- function rutime($ru, $rus, $index) {
- return ($ru["ru_$index.tv_sec"]*1000 + (int)($ru["ru_$index.tv_usec"]/1000))
- - ($rus["ru_$index.tv_sec"]*1000 + (int)($rus["ru_$index.tv_usec"]/1000));
- }
- $ru = getrusage();
- echo "This process used " . rutime($ru, $rustart, "utime") .
- " ms for its computations\n";
- echo "It spent " . rutime($ru, $rustart, "stime") .
- " ms in system calls\n";
Advertisement
Add Comment
Please, Sign In to add comment