Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function print_points($points)
- {
- $xc = array_column($points, 'x');
- $xmin = min($xc);
- $xmax = max($xc);
- $xdiff = $xmax - $xmin;
- $yc = array_column($points, 'y');
- $ymin = min($yc);
- $ymax = max($yc);
- $ydiff = $ymax - $ymin;
- $print = array_fill(0, $ydiff + 1, array_fill(0, $xdiff + 1, ' '));
- foreach ($points as $p) {
- $px = $p['x'] - $xmin;
- $py = $p['y'] - $ymin;
- $print[$py][$px] = '#';
- }
- foreach ($print as $py) {
- echo implode('', $py);
- echo PHP_EOL;
- }
- }
- function find_smallest_box(&$points, $skip = 1)
- {
- foreach ($points as &$p) {
- $p['x'] += $p['vx'] * $skip;
- $p['y'] += $p['vy'] * $skip;
- }
- $xc = array_column($points, 'x');
- $xmin = min($xc);
- $xmax = max($xc);
- $xdiff = $xmax - $xmin;
- $yc = array_column($points, 'y');
- $ymin = min($yc);
- $ymax = max($yc);
- $ydiff = $ymax - $ymin;
- return $xdiff * $ydiff;
- }
- $input_lines = file('input.txt');
- $points = array();
- $smin = PHP_INT_MAX;
- $smax = 0;
- foreach ($input_lines as $input_line) {
- preg_match_all('/.*?<\s*?(-?\d+),\s*?(-?\d+)>.*?<\s*?(-?\d+),\s*?(-?\d+)>/', $input_line, $match);
- $point = array('x' => (int)$match[1][0], 'y' => (int)$match[2][0], 'vx' => (int)$match[3][0], 'vy' => (int)$match[4][0]);
- $points[] = $point;
- $dx = abs($point['x']) / abs($point['vx']);
- $dy = abs($point['y']) / abs($point['vy']);
- $smin = min($smin, $dx, $dy);
- $smax = max($smax, $dx, $dy);
- }
- $smallest = PHP_INT_MAX;
- $smallest_sec = 0;
- $spoints = array();
- $s = find_smallest_box($points, $smin);
- for ($i = $smin; $i < $smax; $i++) {
- $s = find_smallest_box($points);
- if ($s < $smallest) {
- $smallest = $s;
- $spoints = $points;
- $ssec = $i + 1;
- } else {
- break;
- }
- }
- print_points($spoints);
- echo $ssec . PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement