Add day 3 part 1

This commit is contained in:
Clement Desmidt 2019-12-06 16:21:53 +01:00
parent 4cefe61056
commit 207d834b60

40
day_3/part_1.php Normal file
View File

@ -0,0 +1,40 @@
<?php
$wire_paths = explode("\n", file_get_contents('input.txt'));
$wires = [];
function mark_wire_in_grid(string $wire)
{
$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";
}
}
return $points;
}
foreach ($wire_paths as $i => $wire_path) {
$wires[] = mark_wire_in_grid($wire_path);
}
$common_points = array_intersect($wires[0], $wires[1]);
foreach ($common_points as $common_point) {
[$x,$y] = array_map('intval', explode(',', $common_point));
echo sprintf('Distance entre %s et %s : %s%s', $x, $y, $x + $y, "\n");
}