// ==================================================
// > PART 1
// ==================================================
$pos = [0, 0];
$dir = 1;
foreach ($input->lines as $line) {
[$inst, $num] = [$line[0], int($line->sub(1))];
if ($inst == "F") $inst = ["N", "E", "S", "W"][$dir];
match ($inst) {
"N" => $pos[1] -= $num,
"S" => $pos[1] += $num,
"E" => $pos[0] += $num,
"W" => $pos[0] -= $num,
"L" => $dir = Math::mod($dir - ($num / 90), 4),
"R" => $dir = Math::mod($dir + ($num / 90), 4),
default => null
};
}
$solution_1 = set($pos)->map("abs")->sum();
// ==================================================
// > PART 2
// ==================================================
$pos = [0, 0];
$wp = [10, -1];
foreach ($input->lines as $line) {
[$inst, $num] = [$line[0], int($line->sub(1))];
match ($inst) {
"F" => $pos = [$pos[0] + $wp[0] * $num, $pos[1] + $wp[1] * $num],
"N" => $wp[1] -= $num,
"S" => $wp[1] += $num,
"E" => $wp[0] += $num,
"W" => $wp[0] -= $num,
"L" => $wp = array_reduce(range(1, $num / 90), fn ($w) => [$w[1], -$w[0]], $wp),
"R" => $wp = array_reduce(range(1, $num / 90), fn ($w) => [-$w[1], $w[0]], $wp),
default => null
};
}
$solution_2 = set($pos)->map("abs")->sum();