Browse Source

Add day 3 part 1

master
Clement Desmidt 2 years ago
parent
commit
207d834b60
  1. 40
      day_3/part_1.php

40
day_3/part_1.php

@ -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");
}
Loading…
Cancel
Save