parent
207d834b60
commit
ecf949607b
1 changed files with 41 additions and 0 deletions
@ -0,0 +1,41 @@ |
||||
<?php |
||||
|
||||
$wire_paths = explode("\n", file_get_contents('input.txt')); |
||||
$wires = []; |
||||
|
||||
function mark_wire_in_grid(string $wire) |
||||
{ |
||||
$steps = 0; |
||||
$mapping = [ |
||||
'U' => [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)); |
Loading…
Reference in new issue