Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1. <?php
  2.  
  3. function print_points($points)
  4. {
  5.     $xc = array_column($points, 'x');
  6.     $xmin = min($xc);
  7.     $xmax = max($xc);
  8.     $xdiff = $xmax - $xmin;
  9.  
  10.     $yc = array_column($points, 'y');
  11.     $ymin = min($yc);
  12.     $ymax = max($yc);
  13.     $ydiff = $ymax - $ymin;
  14.  
  15.     $print = array_fill(0, $ydiff + 1, array_fill(0, $xdiff + 1, ' '));
  16.  
  17.     foreach ($points as $p) {
  18.         $px = $p['x'] - $xmin;
  19.         $py = $p['y'] - $ymin;
  20.  
  21.         $print[$py][$px] = '#';
  22.     }
  23.  
  24.  
  25.     foreach ($print as $py) {
  26.         echo implode('', $py);
  27.         echo PHP_EOL;
  28.     }
  29. }
  30.  
  31. function find_smallest_box(&$points, $skip = 1)
  32. {
  33.     foreach ($points as &$p) {
  34.         $p['x'] += $p['vx'] * $skip;
  35.         $p['y'] += $p['vy'] * $skip;
  36.     }
  37.  
  38.     $xc = array_column($points, 'x');
  39.     $xmin = min($xc);
  40.     $xmax = max($xc);
  41.     $xdiff = $xmax - $xmin;
  42.  
  43.     $yc = array_column($points, 'y');
  44.     $ymin = min($yc);
  45.     $ymax = max($yc);
  46.     $ydiff = $ymax - $ymin;
  47.  
  48.     return $xdiff * $ydiff;
  49. }
  50.  
  51. $input_lines = file('input.txt');
  52.  
  53. $points = array();
  54. $smin = PHP_INT_MAX;
  55. $smax = 0;
  56.  
  57. foreach ($input_lines as $input_line) {
  58.     preg_match_all('/.*?<\s*?(-?\d+),\s*?(-?\d+)>.*?<\s*?(-?\d+),\s*?(-?\d+)>/', $input_line, $match);
  59.  
  60.     $point = array('x' => (int)$match[1][0], 'y' => (int)$match[2][0], 'vx' => (int)$match[3][0], 'vy' => (int)$match[4][0]);
  61.     $points[] = $point;
  62.  
  63.     $dx = abs($point['x']) / abs($point['vx']);
  64.     $dy = abs($point['y']) / abs($point['vy']);
  65.  
  66.     $smin = min($smin, $dx, $dy);
  67.     $smax = max($smax, $dx, $dy);
  68. }
  69.  
  70. $smallest = PHP_INT_MAX;
  71. $smallest_sec = 0;
  72.  
  73. $spoints = array();
  74.  
  75. $s = find_smallest_box($points, $smin);
  76.  
  77. for ($i = $smin; $i < $smax; $i++) {
  78.     $s = find_smallest_box($points);
  79.     if ($s < $smallest) {
  80.         $smallest = $s;
  81.         $spoints = $points;
  82.         $ssec = $i + 1;
  83.     } else {
  84.         break;
  85.     }
  86. }
  87.  
  88. print_points($spoints);
  89. echo $ssec . PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement