SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $cable_a = 'R8,U5,L5,D3';
  2. $cable_b = 'U7,R6,D4,L4';
  3.  
  4. $min_distance = -1;
  5.  
  6. function travelCable($cable, &$raster, &$intersections, $indicator)
  7. {
  8.   $pointer_x = 0;
  9.   $pointer_y = 0;
  10.  
  11.   $raster[$pointer_x][$pointer_y] = 'o';
  12.  
  13.   foreach (explode(',', $cable) as $instruction) {
  14.     $direction = substr($instruction, 0, 1);
  15.     $amount = substr($instruction, 1);
  16.  
  17.     for ($a = 0; $a < $amount; $a++) {
  18.       switch ($direction) {
  19.         case 'U':
  20.           $pointer_y++;
  21.           break;
  22.         case 'D':
  23.           $pointer_y--;
  24.           break;
  25.         case 'L':
  26.           $pointer_x--;
  27.           break;
  28.         case 'R':
  29.           $pointer_x++;
  30.           break;
  31.       }
  32.       if (!empty($raster[$pointer_x][$pointer_y])) {
  33.         $intersections[$pointer_x][$pointer_y] = 1;
  34.       } else {
  35.         $raster[$pointer_x][$pointer_y] = $indicator;
  36.       }
  37.     }
  38.   }
  39. }
  40.  
  41. travelCable($cable_a, $raster, $intersections, 'a');
  42. travelCable($cable_b, $raster, $intersections, 'b');
  43.  
  44. function distance($vector1, $vector2)
  45. {
  46.   $n = count($vector1);
  47.   $sum = 0;
  48.   for ($i = 0; $i < $n; $i++) {
  49.     $sum += abs($vector1[$i] - $vector2[$i]);
  50.   }
  51.   return $sum;
  52. }
  53.  
  54. foreach ($intersections as $x => $intersection) {
  55.   foreach ($intersection as $y => $value) {
  56.     $distance = distance([0, 0], [$x, $y]);
  57.     if ($min_distance < 0 || $distance < $min_distance) {
  58.       $min_distance = $distance;
  59.     }
  60.     $intersections[$x][$y] = $distance;
  61.     $raster[$x][$y] = 'x';
  62.   }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top