Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $cable_a = 'R8,U5,L5,D3';
- $cable_b = 'U7,R6,D4,L4';
- $min_distance = -1;
- function travelCable($cable, &$raster, &$intersections, $indicator)
- {
- $pointer_x = 0;
- $pointer_y = 0;
- $raster[$pointer_x][$pointer_y] = 'o';
- foreach (explode(',', $cable) as $instruction) {
- $direction = substr($instruction, 0, 1);
- $amount = substr($instruction, 1);
- for ($a = 0; $a < $amount; $a++) {
- switch ($direction) {
- case 'U':
- $pointer_y++;
- break;
- case 'D':
- $pointer_y--;
- break;
- case 'L':
- $pointer_x--;
- break;
- case 'R':
- $pointer_x++;
- break;
- }
- if (!empty($raster[$pointer_x][$pointer_y])) {
- $intersections[$pointer_x][$pointer_y] = 1;
- } else {
- $raster[$pointer_x][$pointer_y] = $indicator;
- }
- }
- }
- }
- travelCable($cable_a, $raster, $intersections, 'a');
- travelCable($cable_b, $raster, $intersections, 'b');
- function distance($vector1, $vector2)
- {
- $n = count($vector1);
- $sum = 0;
- for ($i = 0; $i < $n; $i++) {
- $sum += abs($vector1[$i] - $vector2[$i]);
- }
- return $sum;
- }
- foreach ($intersections as $x => $intersection) {
- foreach ($intersection as $y => $value) {
- $distance = distance([0, 0], [$x, $y]);
- if ($min_distance < 0 || $distance < $min_distance) {
- $min_distance = $distance;
- }
- $intersections[$x][$y] = $distance;
- $raster[$x][$y] = 'x';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement