Answer ⏱ 5ms
Part 1 :
48795
Part 2 :
40654918441248
function solve($dirs) {
$x1 = $y1 = $x2 = $y2 = $border = $area = 0;
foreach ($dirs as [$dir, $length]) {
$border += $length;
match ($dir) {
"U" => $y2 -= $length,
"D" => $y2 += $length,
"L" => $x2 -= $length,
"R" => $x2 += $length,
};
$area += ($y1 + $y2) * ($x1 - $x2);
[$x1, $y1] = [$x2, $y2];
}
return ($area + $border) / 2 + 1;
}
$solution_1 = solve($input->lines->map(fn ($l) => explode(" ", $l)));
$solution_2 = solve($input->lines->map(fn ($line) => [
["R", "D", "L", "U"][substr($line, -2, 1)],
hexdec(substr($line, -7, 5))
]));