From ecf949607b9b7960bdae995d061e3222f6b0ac51 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Fri, 6 Dec 2019 16:35:31 +0100 Subject: [PATCH] :sparkles: Add day 3 part 2 --- day_3/part_2.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 day_3/part_2.php diff --git a/day_3/part_2.php b/day_3/part_2.php new file mode 100644 index 0000000..f5b2206 --- /dev/null +++ b/day_3/part_2.php @@ -0,0 +1,41 @@ + [0,1], + 'D' => [0,-1], + 'L' => [-1,0], + 'R' => [1,0], + ]; + $points = []; + $path = explode(',', $wire); + $current_point_x = 0; + $current_point_y = 0; + foreach ($path as $dot) { + $direction = $dot[0]; + $number = (int) substr($dot, 1); + for ($t = 0; $t < $number; $t++) { + $current_point_x += $mapping[$direction][0]; + $current_point_y += $mapping[$direction][1]; + $points["$current_point_x,$current_point_y"] = ++$steps; + } + } + + return $points; +} + +foreach ($wire_paths as $i => $wire_path) { + $wires[] = mark_wire_in_grid($wire_path); +} + +$common_points = array_intersect_key($wires[0], $wires[1]); +$total_steps = []; +foreach ($common_points as $coordinates => $steps) { + $total_steps[$coordinates] = $wires[0][$coordinates] + $wires[1][$coordinates]; +} +echo sprintf('Le plus court pas : %s', min($total_steps));